Skip to content

Commit 1be6ee3

Browse files
authored
Merge pull request #694 from DeaneOC/Pretty-Print-XML-Functionality
Pretty print XML
2 parents d51250f + bf92193 commit 1be6ee3

File tree

4 files changed

+1715
-14
lines changed

4 files changed

+1715
-14
lines changed

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

+112-7
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,28 @@ 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+
/**
759+
* Convert a JSONObject into a well-formed, element-normal XML string,
760+
* either pretty print or single-lined depending on indent factor.
761+
*
762+
* @param object
763+
* A JSONObject.
764+
* @param tagName
765+
* The optional name of the enclosing tag.
766+
* @param config
767+
* Configuration that can control output to XML.
768+
* @param indentFactor
769+
* The number of spaces to add to each level of indentation.
770+
* @param indent
771+
* The current ident level in spaces.
772+
* @return
773+
* @throws JSONException
774+
*/
775+
private static String toString(final Object object, final String tagName, final XMLParserConfiguration config, int indentFactor, int indent)
776+
throws JSONException {
755777
StringBuilder sb = new StringBuilder();
756778
JSONArray ja;
757779
JSONObject jo;
@@ -761,9 +783,14 @@ public static String toString(final Object object, final String tagName, final X
761783

762784
// Emit <tagName>
763785
if (tagName != null) {
786+
sb.append(indent(indent));
764787
sb.append('<');
765788
sb.append(tagName);
766789
sb.append('>');
790+
if(indentFactor > 0){
791+
sb.append("\n");
792+
indent += indentFactor;
793+
}
767794
}
768795

769796
// Loop thru the keys.
@@ -806,31 +833,39 @@ public static String toString(final Object object, final String tagName, final X
806833
sb.append('<');
807834
sb.append(key);
808835
sb.append('>');
809-
sb.append(toString(val, null, config));
836+
sb.append(toString(val, null, config, indentFactor, indent));
810837
sb.append("</");
811838
sb.append(key);
812839
sb.append('>');
813840
} else {
814-
sb.append(toString(val, key, config));
841+
sb.append(toString(val, key, config, indentFactor, indent));
815842
}
816843
}
817844
} else if ("".equals(value)) {
845+
sb.append(indent(indent));
818846
sb.append('<');
819847
sb.append(key);
820848
sb.append("/>");
849+
if(indentFactor > 0){
850+
sb.append("\n");
851+
}
821852

822853
// Emit a new tag <k>
823854

824855
} else {
825-
sb.append(toString(value, key, config));
856+
sb.append(toString(value, key, config, indentFactor, indent));
826857
}
827858
}
828859
if (tagName != null) {
829860

830861
// Emit the </tagName> close tag
862+
sb.append(indent(indent - indentFactor));
831863
sb.append("</");
832864
sb.append(tagName);
833865
sb.append('>');
866+
if(indentFactor > 0){
867+
sb.append("\n");
868+
}
834869
}
835870
return sb.toString();
836871

@@ -849,15 +884,85 @@ public static String toString(final Object object, final String tagName, final X
849884
// XML does not have good support for arrays. If an array
850885
// appears in a place where XML is lacking, synthesize an
851886
// <array> element.
852-
sb.append(toString(val, tagName == null ? "array" : tagName, config));
887+
sb.append(toString(val, tagName == null ? "array" : tagName, config, indentFactor, indent));
853888
}
854889
return sb.toString();
855890
}
856891

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

895+
if(tagName == null){
896+
return indent(indent) + "\"" + string + "\"" + ((indentFactor > 0) ? "\n" : "");
897+
} else if(string.length() == 0){
898+
return indent(indent) + "<" + tagName + "/>" + ((indentFactor > 0) ? "\n" : "");
899+
} else {
900+
return indent(indent) + "<" + tagName
901+
+ ">" + string + "</" + tagName + ">" + ((indentFactor > 0) ? "\n" : "");
902+
}
903+
}
904+
905+
/**
906+
* Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
907+
*
908+
* @param object
909+
* A JSONObject.
910+
* @param indentFactor
911+
* The number of spaces to add to each level of indentation.
912+
* @return A string.
913+
* @throws JSONException Thrown if there is an error parsing the string
914+
*/
915+
public static String toString(Object object, int indentFactor){
916+
return toString(object, null, XMLParserConfiguration.ORIGINAL, indentFactor);
917+
}
918+
919+
/**
920+
* Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
921+
*
922+
* @param object
923+
* A JSONObject.
924+
* @param tagName
925+
* The optional name of the enclosing tag.
926+
* @param indentFactor
927+
* The number of spaces to add to each level of indentation.
928+
* @return A string.
929+
* @throws JSONException Thrown if there is an error parsing the string
930+
*/
931+
public static String toString(final Object object, final String tagName, int indentFactor) {
932+
return toString(object, tagName, XMLParserConfiguration.ORIGINAL, indentFactor);
933+
}
934+
935+
/**
936+
* Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
937+
*
938+
* @param object
939+
* A JSONObject.
940+
* @param tagName
941+
* The optional name of the enclosing tag.
942+
* @param config
943+
* Configuration that can control output to XML.
944+
* @param indentFactor
945+
* The number of spaces to add to each level of indentation.
946+
* @return A string.
947+
* @throws JSONException Thrown if there is an error parsing the string
948+
*/
949+
public static String toString(final Object object, final String tagName, final XMLParserConfiguration config, int indentFactor)
950+
throws JSONException {
951+
return toString(object, tagName, config, indentFactor, 0);
952+
}
953+
954+
/**
955+
* Return a String consisting of a number of space characters specified by indent
956+
*
957+
* @param indent
958+
* The number of spaces to be appended to the String.
959+
* @return
960+
*/
961+
private static final String indent(int indent) {
962+
StringBuilder sb = new StringBuilder();
963+
for (int i = 0; i < indent; i++) {
964+
sb.append(' ');
965+
}
966+
return sb.toString();
862967
}
863968
}

0 commit comments

Comments
 (0)