XmlUtil.mapToXml support List

This commit is contained in:
Looly
2020-04-09 16:35:44 +08:00
parent 4210ebaa4e
commit ced42166d3
4 changed files with 166 additions and 57 deletions

View File

@@ -14,12 +14,11 @@ import java.util.Map;
/**
* {@link XmlUtil} 工具类
*
* @author Looly
*
* @author Looly
*/
public class XmlUtilTest {
@Test
public void parseTest() {
String result = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>"//
@@ -84,7 +83,7 @@ public class XmlUtilTest {
Assert.assertEquals("1490", map.get("remainpoint"));
Assert.assertEquals("885", map.get("taskID"));
Assert.assertEquals("1", map.get("successCounts"));
Assert.assertEquals("subText", ((Map<?, ?>)map.get("newNode")).get("sub"));
Assert.assertEquals("subText", ((Map<?, ?>) map.get("newNode")).get("sub"));
}
@Test
@@ -106,17 +105,33 @@ public class XmlUtilTest {
Document doc = XmlUtil.mapToXml(map, "user");
// Console.log(XmlUtil.toStr(doc, false));
Assert.assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>"//
+ "<user>"//
+ "<name>张三</name>"//
+ "<age>12</age>"//
+ "<game>"//
+ "<昵称>Looly</昵称>"//
+ "<level>14</level>"//
+ "</game>"//
+ "</user>", //
+ "<user>"//
+ "<name>张三</name>"//
+ "<age>12</age>"//
+ "<game>"//
+ "<昵称>Looly</昵称>"//
+ "<level>14</level>"//
+ "</game>"//
+ "</user>", //
XmlUtil.toStr(doc, false));
}
@Test
public void mapToXmlTest2() {
// 测试List
Map<String, Object> map = MapBuilder.create(new LinkedHashMap<String, Object>())
.put("Town", CollUtil.newArrayList("town1", "town2"))
.build();
Document doc = XmlUtil.mapToXml(map, "City");
Assert.assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" +
"<City>" +
"<Town>town1</Town>" +
"<Town>town2</Town>" +
"</City>",
XmlUtil.toStr(doc));
}
@Test
public void readTest() {
Document doc = XmlUtil.readXML("test.xml");
@@ -127,9 +142,9 @@ public class XmlUtilTest {
public void mapToXmlTestWithOmitXmlDeclaration() {
Map<String, Object> map = MapBuilder.create(new LinkedHashMap<String, Object>())
.put("name", "ddatsh")
.build();
String xml=XmlUtil.mapToXmlStr(map,true);
Assert.assertEquals(xml,"<xml><name>ddatsh</name></xml>");
}
.put("name", "ddatsh")
.build();
String xml = XmlUtil.mapToXmlStr(map, true);
Assert.assertEquals("<xml><name>ddatsh</name></xml>", xml);
}
}