@@ -63,6 +63,7 @@ public final class Xml {
6363 private static final String QUOT = """ ;
6464 private static final String XML_HEADER = "<?xml " ;
6565 private static final String DOCTYPE_TEXT = "!DOCTYPE" ;
66+ private static final String ROOT = "root" ;
6667 private static final String DOCTYPE_HEADER = "<" + DOCTYPE_TEXT + " " ;
6768 private static final java .nio .charset .Charset UTF_8 = java .nio .charset .Charset .forName ("UTF-8" );
6869 private static final java .util .regex .Pattern ATTRS = java .util .regex .Pattern .compile (
@@ -795,7 +796,7 @@ private static void escape(String s, StringBuilder sb) {
795796 sb .append ("\n " );
796797 break ;
797798 case '\r' :
798- sb .append ("\\ r " );
799+ sb .append ("
 " );
799800 break ;
800801 case '\t' :
801802 sb .append ("\t " );
@@ -889,6 +890,10 @@ public static String toXml(Collection collection) {
889890 }
890891
891892 public static String toXml (Map map , XmlStringBuilder .Step identStep ) {
893+ return toXml (map , identStep , ROOT );
894+ }
895+
896+ public static String toXml (Map map , XmlStringBuilder .Step identStep , String newRootName ) {
892897 final XmlStringBuilder builder ;
893898 final Map localMap ;
894899 if (map != null && map .containsKey (ENCODING )) {
@@ -907,11 +912,11 @@ public static String toXml(Map map, XmlStringBuilder.Step identStep) {
907912 builder = new XmlStringBuilderWithoutRoot (identStep , UTF_8 .name (), "" );
908913 localMap = map ;
909914 }
910- checkLocalMap (builder , localMap );
915+ checkLocalMap (builder , localMap , newRootName );
911916 return builder .toString ();
912917 }
913918
914- private static void checkLocalMap (final XmlStringBuilder builder , final Map localMap ) {
919+ private static void checkLocalMap (final XmlStringBuilder builder , final Map localMap , final String newRootName ) {
915920 final Map localMap2 ;
916921 if (localMap != null && localMap .containsKey (DOCTYPE_TEXT )) {
917922 localMap2 = (Map ) U .clone (localMap );
@@ -926,11 +931,12 @@ private static void checkLocalMap(final XmlStringBuilder builder, final Map loca
926931 if ("root" .equals (XmlValue .getMapKey (localMap2 ))) {
927932 writeArray ((List ) XmlValue .getMapValue (localMap2 ), builder );
928933 } else {
929- XmlObject .writeXml (localMap2 , getRootName (localMap2 ), builder , false ,
934+ XmlObject .writeXml (localMap2 , getRootName (localMap2 , newRootName ), builder , false ,
930935 U .<String >newLinkedHashSet (), false );
931936 }
932937 } else {
933- XmlObject .writeXml (localMap2 , getRootName (localMap2 ), builder , false , U .<String >newLinkedHashSet (), false );
938+ XmlObject .writeXml (localMap2 , getRootName (localMap2 , newRootName ), builder ,
939+ false , U .<String >newLinkedHashSet (), false );
934940 }
935941 }
936942
@@ -964,7 +970,7 @@ private static XmlStringBuilder checkStandalone(String encoding, XmlStringBuilde
964970 }
965971
966972 @ SuppressWarnings ("unchecked" )
967- private static String getRootName (final Map localMap ) {
973+ private static String getRootName (final Map localMap , final String newRootName ) {
968974 int foundAttrs = 0 ;
969975 int foundElements = 0 ;
970976 int foundListElements = 0 ;
@@ -982,15 +988,15 @@ private static String getRootName(final Map localMap) {
982988 }
983989 }
984990 }
985- return foundAttrs == 0 && foundElements == 1 && foundListElements == 0 ? null : "root" ;
991+ return foundAttrs == 0 && foundElements == 1 && foundListElements == 0 ? null : newRootName ;
986992 }
987993
988994 public static String toXml (Map map ) {
989- return toXml (map , XmlStringBuilder .Step .TWO_SPACES );
995+ return toXml (map , XmlStringBuilder .Step .TWO_SPACES , ROOT );
990996 }
991997
992998 @ SuppressWarnings ("unchecked" )
993- private static Object getValue (final Object value , final FromType fromType ) {
999+ private static Object getValue (final String name , final Object value , final FromType fromType ) {
9941000 final Object localValue ;
9951001 if (value instanceof Map && ((Map <String , Object >) value ).entrySet ().size () == 1 ) {
9961002 final Map .Entry <String , Object > entry = ((Map <String , Object >) value ).entrySet ().iterator ().next ();
@@ -1003,7 +1009,8 @@ private static Object getValue(final Object value, final FromType fromType) {
10031009 } else {
10041010 localValue = value ;
10051011 }
1006- return localValue instanceof String ? XmlValue .unescape ((String ) localValue ) : localValue ;
1012+ return localValue instanceof String && name .startsWith ("-" )
1013+ ? XmlValue .unescape ((String ) localValue ) : localValue ;
10071014 }
10081015
10091016 public static Object stringToNumber (String number ) {
@@ -1099,9 +1106,9 @@ private static Object checkArray(final Map<String, Object> map, final String nam
10991106 localMap4 .remove (ARRAY );
11001107 localMap4 .remove (SELF_CLOSING );
11011108 object = name .equals (XmlValue .getMapKey (localMap4 ))
1102- ? U .newArrayList (Collections .singletonList (getValue (XmlValue .getMapValue (localMap4 ),
1109+ ? U .newArrayList (Collections .singletonList (getValue (name , XmlValue .getMapValue (localMap4 ),
11031110 FromType .FOR_CONVERT )))
1104- : U .newArrayList (Collections .singletonList (getValue (localMap4 , FromType .FOR_CONVERT )));
1111+ : U .newArrayList (Collections .singletonList (getValue (name , localMap4 , FromType .FOR_CONVERT )));
11051112 } else {
11061113 object = localMap ;
11071114 }
@@ -1250,13 +1257,13 @@ private static void addNodeValue(final Map<String, Object> map, final String nam
12501257 final String elementName = unescapeName (elementMapper .apply (name , namespaces ));
12511258 if (map .containsKey (elementName )) {
12521259 if (TEXT .equals (elementName )) {
1253- map .put (elementName + uniqueIds [0 ], nodeMapper .apply (getValue (value , fromType )));
1260+ map .put (elementName + uniqueIds [0 ], nodeMapper .apply (getValue (name , value , fromType )));
12541261 uniqueIds [0 ] += 1 ;
12551262 } else if (COMMENT .equals (elementName )) {
1256- map .put (elementName + uniqueIds [1 ], nodeMapper .apply (getValue (value , fromType )));
1263+ map .put (elementName + uniqueIds [1 ], nodeMapper .apply (getValue (name , value , fromType )));
12571264 uniqueIds [1 ] += 1 ;
12581265 } else if (CDATA .equals (elementName )) {
1259- map .put (elementName + uniqueIds [2 ], nodeMapper .apply (getValue (value , fromType )));
1266+ map .put (elementName + uniqueIds [2 ], nodeMapper .apply (getValue (name , value , fromType )));
12601267 uniqueIds [2 ] += 1 ;
12611268 } else {
12621269 final Object object = map .get (elementName );
@@ -1271,7 +1278,7 @@ private static void addNodeValue(final Map<String, Object> map, final String nam
12711278 }
12721279 } else {
12731280 if (elementName != null ) {
1274- map .put (elementName , nodeMapper .apply (getValue (value , fromType )));
1281+ map .put (elementName , nodeMapper .apply (getValue (name , value , fromType )));
12751282 }
12761283 }
12771284 }
@@ -1292,7 +1299,7 @@ private static void addText(final Map<String, Object> map, final String name, fi
12921299 objects .add (index , item );
12931300 lastIndex -= 1 ;
12941301 }
1295- final Object newValue = getValue (value , fromType );
1302+ final Object newValue = getValue (name , value , fromType );
12961303 if (newValue instanceof List ) {
12971304 objects .add (((List ) newValue ).get (0 ));
12981305 } else {
@@ -1340,7 +1347,7 @@ private static boolean checkResult(final String xml, org.w3c.dom.Document docume
13401347 } else if (headerAttributes .containsKey (STANDALONE .substring (1 ))) {
13411348 ((Map ) result ).put (STANDALONE , headerAttributes .get (STANDALONE .substring (1 )));
13421349 } else if (fromType == FromType .FOR_CONVERT
1343- && ((Map .Entry ) ((Map ) result ).entrySet ().iterator ().next ()).getKey ().equals ("root" )
1350+ && ((Map .Entry ) ((Map ) result ).entrySet ().iterator ().next ()).getKey ().equals (ROOT )
13441351 && (((Map .Entry ) ((Map ) result ).entrySet ().iterator ().next ()).getValue () instanceof List
13451352 || ((Map .Entry ) ((Map ) result ).entrySet ().iterator ().next ()).getValue () instanceof Map )) {
13461353 if (xml .startsWith (XML_HEADER )) {
@@ -1498,7 +1505,7 @@ public static Object fromXmlWithoutNamespacesAndAttributes(final String xml) {
14981505
14991506 public static String formatXml (String xml , XmlStringBuilder .Step identStep ) {
15001507 Object result = fromXml (xml , FromType .FOR_FORMAT );
1501- return toXml ((Map ) result , identStep );
1508+ return toXml ((Map ) result , identStep , ROOT );
15021509 }
15031510
15041511 public static String formatXml (String xml ) {
@@ -1511,7 +1518,7 @@ public static String changeXmlEncoding(String xml, XmlStringBuilder.Step identSt
15111518 if (result instanceof Map ) {
15121519 ((Map ) result ).put (ENCODING , encoding );
15131520 }
1514- return toXml ((Map ) result , identStep );
1521+ return toXml ((Map ) result , identStep , ROOT );
15151522 }
15161523
15171524 public static String changeXmlEncoding (String xml , String encoding ) {
0 commit comments