@@ -1745,6 +1745,8 @@ public static String toJson(Map map) {
17451745 public static class XmlStringBuilder {
17461746 protected final StringBuilder builder ;
17471747 private int ident ;
1748+ private int saveAttrPosition ;
1749+ private int saveTextPosition ;
17481750
17491751 public XmlStringBuilder () {
17501752 builder = new StringBuilder ("<?xml version=\" 1.0\" encoding=\" UTF-8\" ?>\n <root>\n " );
@@ -1757,10 +1759,32 @@ public XmlStringBuilder(StringBuilder builder, int ident) {
17571759 }
17581760
17591761 public XmlStringBuilder append (final String string ) {
1762+ if (">" .equals (string )) {
1763+ saveAttrPosition = builder .length ();
1764+ saveTextPosition = builder .length () + 1 ;
1765+ }
17601766 builder .append (string );
17611767 return this ;
17621768 }
17631769
1770+ public XmlStringBuilder insert (final String key , final String value ) {
1771+ builder .insert (saveAttrPosition , " " + key + "=\" " );
1772+ saveAttrPosition += (" " + key + "=\" " ).length ();
1773+ saveTextPosition += (" " + key + "=\" " ).length ();
1774+ builder .insert (saveAttrPosition , value + "\" " );
1775+ saveAttrPosition += (value + "\" " ).length ();
1776+ saveTextPosition += (value + "\" " ).length ();
1777+ return this ;
1778+ }
1779+
1780+ public XmlStringBuilder insert (final String value ) {
1781+ builder .delete (saveTextPosition , saveTextPosition + 1 );
1782+ builder .insert (saveTextPosition , value );
1783+ saveTextPosition += value .length ();
1784+ saveAttrPosition += value .length ();
1785+ return this ;
1786+ }
1787+
17641788 public XmlStringBuilder fillSpaces () {
17651789 for (int index = 0 ; index < ident ; index += 1 ) {
17661790 builder .append (' ' );
@@ -1985,13 +2009,26 @@ public static void writeXml(Map map, XmlStringBuilder builder) {
19852009 }
19862010
19872011 Iterator iter = map .entrySet ().iterator ();
2012+ boolean textWasInserted = false ;
19882013 while (iter .hasNext ()) {
19892014 Map .Entry entry = (Map .Entry ) iter .next ();
1990- builder .fillSpaces ().append ("<" ).append (escape (String .valueOf (entry .getKey ()))).append (">" );
1991- XmlValue .writeXml (entry .getValue (), builder );
1992- builder .append ("</" ).append (escape (String .valueOf (entry .getKey ()))).append (">" );
1993- if (iter .hasNext ()) {
1994- builder .newLine ();
2015+ if (escape (String .valueOf (entry .getKey ())).startsWith ("-" ) && entry .getValue () instanceof String ) {
2016+ builder .insert (escape (String .valueOf (entry .getKey ())).substring (1 ), escape ((String ) entry .getValue ()));
2017+ } else if ("#text" .equals (escape (String .valueOf (entry .getKey ())))) {
2018+ builder .insert (escape ((String ) entry .getValue ()));
2019+ textWasInserted = true ;
2020+ } else {
2021+ if (textWasInserted ) {
2022+ textWasInserted = false ;
2023+ } else {
2024+ builder .fillSpaces ();
2025+ }
2026+ builder .append ("<" ).append (escape (String .valueOf (entry .getKey ()))).append (">" );
2027+ XmlValue .writeXml (entry .getValue (), builder );
2028+ builder .append ("</" ).append (escape (String .valueOf (entry .getKey ()))).append (">" );
2029+ if (iter .hasNext ()) {
2030+ builder .newLine ();
2031+ }
19952032 }
19962033 }
19972034 }
0 commit comments