X Tutup
Skip to content

Commit b7f708b

Browse files
committed
Altered XML toString to allow indentation param
1 parent 8439039 commit b7f708b

File tree

2 files changed

+166
-7
lines changed

2 files changed

+166
-7
lines changed

src/main/java/org/json/XML.java

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,11 @@ public static String toString(final Object object, final String tagName) {
752752
*/
753753
public static String toString(final Object object, final String tagName, final XMLParserConfiguration config)
754754
throws JSONException {
755+
return toString(object, tagName, config, 0, 0);
756+
}
757+
758+
private static String toString(final Object object, final String tagName, final XMLParserConfiguration config, int indentFactor, int indent)
759+
throws JSONException {
755760
StringBuilder sb = new StringBuilder();
756761
JSONArray ja;
757762
JSONObject jo;
@@ -761,9 +766,14 @@ public static String toString(final Object object, final String tagName, final X
761766

762767
// Emit <tagName>
763768
if (tagName != null) {
769+
sb.append(indent(indent));
764770
sb.append('<');
765771
sb.append(tagName);
766772
sb.append('>');
773+
if(indentFactor > 0){
774+
sb.append("\n");
775+
indent += indentFactor;
776+
}
767777
}
768778

769779
// Loop thru the keys.
@@ -806,31 +816,39 @@ public static String toString(final Object object, final String tagName, final X
806816
sb.append('<');
807817
sb.append(key);
808818
sb.append('>');
809-
sb.append(toString(val, null, config));
819+
sb.append(toString(val, null, config, indentFactor, indent));
810820
sb.append("</");
811821
sb.append(key);
812822
sb.append('>');
813823
} else {
814-
sb.append(toString(val, key, config));
824+
sb.append(toString(val, key, config, indentFactor, indent));
815825
}
816826
}
817827
} else if ("".equals(value)) {
828+
sb.append(indent(indent));
818829
sb.append('<');
819830
sb.append(key);
820831
sb.append("/>");
832+
if(indentFactor > 0){
833+
sb.append("\n");
834+
}
821835

822836
// Emit a new tag <k>
823837

824838
} else {
825-
sb.append(toString(value, key, config));
839+
sb.append(toString(value, key, config, indentFactor, indent));
826840
}
827841
}
828842
if (tagName != null) {
829843

830844
// Emit the </tagName> close tag
845+
sb.append(indent(indent - indentFactor));
831846
sb.append("</");
832847
sb.append(tagName);
833848
sb.append('>');
849+
if(indentFactor > 0){
850+
sb.append("\n");
851+
}
834852
}
835853
return sb.toString();
836854

@@ -849,15 +867,78 @@ public static String toString(final Object object, final String tagName, final X
849867
// XML does not have good support for arrays. If an array
850868
// appears in a place where XML is lacking, synthesize an
851869
// <array> element.
852-
sb.append(toString(val, tagName == null ? "array" : tagName, config));
870+
sb.append(toString(val, tagName == null ? "array" : tagName, config, indentFactor, indent));
853871
}
854872
return sb.toString();
855873
}
856874

875+
857876
string = (object == null) ? "null" : escape(object.toString());
858-
return (tagName == null) ? "\"" + string + "\""
859-
: (string.length() == 0) ? "<" + tagName + "/>" : "<" + tagName
860-
+ ">" + string + "</" + tagName + ">";
861877

878+
if(tagName == null){
879+
return indent(indent) + "\"" + string + "\"" + ((indentFactor > 0) ? "\n" : "");
880+
} else if(string.length() == 0){
881+
return indent(indent) + "<" + tagName + "/>" + ((indentFactor > 0) ? "\n" : "");
882+
} else {
883+
return indent(indent) + "<" + tagName
884+
+ ">" + string + "</" + tagName + ">" + ((indentFactor > 0) ? "\n" : "");
885+
}
886+
}
887+
888+
/**
889+
* Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
890+
*
891+
* @param object
892+
* A JSONObject.
893+
* @param indentFactor
894+
* The number of spaces to add to each level of indentation.
895+
* @return A string.
896+
* @throws JSONException Thrown if there is an error parsing the string
897+
*/
898+
public static String toString(Object object, int indentFactor){
899+
return toString(object, null, XMLParserConfiguration.ORIGINAL, indentFactor);
900+
}
901+
902+
/**
903+
* Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
904+
*
905+
* @param object
906+
* A JSONObject.
907+
* @param tagName
908+
* The optional name of the enclosing tag.
909+
* @param indentFactor
910+
* The number of spaces to add to each level of indentation.
911+
* @return A string.
912+
* @throws JSONException Thrown if there is an error parsing the string
913+
*/
914+
public static String toString(final Object object, final String tagName, int indentFactor) {
915+
return toString(object, tagName, XMLParserConfiguration.ORIGINAL, indentFactor);
916+
}
917+
918+
/**
919+
* Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
920+
*
921+
* @param object
922+
* A JSONObject.
923+
* @param tagName
924+
* The optional name of the enclosing tag.
925+
* @param config
926+
* Configuration that can control output to XML.
927+
* @param indentFactor
928+
* The number of spaces to add to each level of indentation.
929+
* @return A string.
930+
* @throws JSONException Thrown if there is an error parsing the string
931+
*/
932+
public static String toString(final Object object, final String tagName, final XMLParserConfiguration config, int indentFactor)
933+
throws JSONException {
934+
return toString(object, tagName, config, indentFactor, 0);
935+
}
936+
937+
private static final String indent(int indent) {
938+
StringBuilder sb = new StringBuilder();
939+
for (int i = 0; i < indent; i++) {
940+
sb.append(' ');
941+
}
942+
return sb.toString();
862943
}
863944
}

src/test/java/org/json/junit/XMLTest.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,4 +1049,82 @@ public void testXSITypeMapNotModifiable() {
10491049
fail("Expected to be unable to modify the config");
10501050
} catch (Exception ignored) { }
10511051
}
1052+
1053+
@Test
1054+
public void testXmlToStringWithIndent(){
1055+
String str = "{\n" +
1056+
" \"success\": true,\n" +
1057+
" \"error\": null,\n" +
1058+
" \"response\": [\n" +
1059+
" {\n" +
1060+
" \"timestamp\": 1664917200,\n" +
1061+
" \"dateTimeISO\": \"2022-10-05T00:00:00+03:00\",\n" +
1062+
" \"loc\": {\n" +
1063+
" \"lat\": 39.91987,\n" +
1064+
" \"long\": 32.85427\n" +
1065+
" },\n" +
1066+
" \"place\": {\n" +
1067+
" \"name\": \"ankara\",\n" +
1068+
" \"state\": \"an\",\n" +
1069+
" \"country\": \"tr\"\n" +
1070+
" },\n" +
1071+
" \"profile\": {\n" +
1072+
" \"tz\": \"Europe/Istanbul\"\n" +
1073+
" },\n" +
1074+
" \"sun\": {\n" +
1075+
" \"rise\": 1664941721,\n" +
1076+
" \"riseISO\": \"2022-10-05T06:48:41+03:00\",\n" +
1077+
" \"set\": 1664983521,\n" +
1078+
" \"setISO\": \"2022-10-05T18:25:21+03:00\",\n" +
1079+
" \"transit\": 1664962621,\n" +
1080+
" \"transitISO\": \"2022-10-05T12:37:01+03:00\",\n" +
1081+
" \"midnightSun\": false,\n" +
1082+
" \"polarNight\": false,\n" +
1083+
" \"twilight\": {\n" +
1084+
" \"civilBegin\": 1664940106,\n" +
1085+
" \"civilBeginISO\": \"2022-10-05T06:21:46+03:00\",\n" +
1086+
" \"civilEnd\": 1664985136,\n" +
1087+
" \"civilEndISO\": \"2022-10-05T18:52:16+03:00\",\n" +
1088+
" \"nauticalBegin\": 1664938227,\n" +
1089+
" \"nauticalBeginISO\": \"2022-10-05T05:50:27+03:00\",\n" +
1090+
" \"nauticalEnd\": 1664987015,\n" +
1091+
" \"nauticalEndISO\": \"2022-10-05T19:23:35+03:00\",\n" +
1092+
" \"astronomicalBegin\": 1664936337,\n" +
1093+
" \"astronomicalBeginISO\": \"2022-10-05T05:18:57+03:00\",\n" +
1094+
" \"astronomicalEnd\": 1664988905,\n" +
1095+
" \"astronomicalEndISO\": \"2022-10-05T19:55:05+03:00\"\n" +
1096+
" }\n" +
1097+
" },\n" +
1098+
" \"moon\": {\n" +
1099+
" \"rise\": 1664976480,\n" +
1100+
" \"riseISO\": \"2022-10-05T16:28:00+03:00\",\n" +
1101+
" \"set\": 1664921520,\n" +
1102+
" \"setISO\": \"2022-10-05T01:12:00+03:00\",\n" +
1103+
" \"transit\": 1664994240,\n" +
1104+
" \"transitISO\": \"2022-10-05T21:24:00+03:00\",\n" +
1105+
" \"underfoot\": 1664949360,\n" +
1106+
" \"underfootISO\": \"2022-10-05T08:56:00+03:00\",\n" +
1107+
" \"phase\": {\n" +
1108+
" \"phase\": 0.3186,\n" +
1109+
" \"name\": \"waxing gibbous\",\n" +
1110+
" \"illum\": 71,\n" +
1111+
" \"age\": 9.41,\n" +
1112+
" \"angle\": 0.55\n" +
1113+
" }\n" +
1114+
" }\n" +
1115+
" }\n" +
1116+
" ]\n" +
1117+
"}" ;
1118+
JSONObject jsonObject = new JSONObject(str);
1119+
String xmlString = XML.toString(jsonObject, "Outer", 1);
1120+
System.out.println(xmlString);
1121+
System.out.println(XML.toIndentedXmlString(xmlString, 2, true));
1122+
1123+
1124+
}
1125+
1126+
10521127
}
1128+
1129+
1130+

0 commit comments

Comments
 (0)
X Tutup