dom4j dependency package
<!--dom4j--> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version> </dependency>
There are many ways to convert map to json string, so fastjson is used here
<!--fastjson Version1.2.78yesFastjsonLast support at releaseJava 1.7的Version--> <dependency> <groupId></groupId> <artifactId>fastjson</artifactId> <version>1.2.78</version> </dependency>
dom4j implements xml to map, and then uses fastjson to convert map to json string
package ; import ; import org.; import org.; import org.; import org.; import ; import ; import ; import ; public class Test { public static void main(String[] args) throws Exception { String xml = "<sites><site><name>RUNOOB</name><url></url></site><site><name>Google</name><url></url></site><site><name>Facebook</name><url></url></site></sites>"; Map<String, Object> map = xml2map(xml); String json = (map); (json); } /** * xml to map without attributes */ public static Map<String, Object> xml2map(String xml) throws DocumentException { // xml must have a root node xml = "<root>" + xml + "</root>"; Document document = (xml); Element root = (); //Root node Map<String, Object> map = xml2map(root); return map; } /** * xml to map without attributes * If it is a sibling element and the label of the sibling element is the same, then these siblings with the same name need to be merged into an array */ private static Map<String, Object> xml2map(Element element) { Map<String, Object> map = new LinkedHashMap<>(); List<Element> list = (); //Get all child nodes if (() > 0) { //There are children for (Element iter : list) { //Transfer all child nodes List<Object> mapList; if (().size() > 0) { //Sub nodes and child nodes Map<String, Object> m = xml2map(iter); //Recursive call if ((()) != null) { //The tag name already exists, indicating that it is an array structure Object obj = (()); if (obj instanceof List) { //The value of the label is an array structure. Take out the array and add new elements. mapList = (List) obj; //Take out the old value (m); //Put new value } else { mapList = new ArrayList<>(); (obj); //Put the old value (m); //Put new value } ((), mapList); } else { ((), m); } } else { //Child nodes have no children if ((()) != null) { //The tag name already exists, indicating that it is an array structure Object obj = (()); if (obj instanceof List) { //The value of the label is an array structure. Take out the array and add new elements. mapList = (List) obj; //Take out the old value (()); //Put new value } else { mapList = new ArrayList<>(); (obj); //Put the old value (()); //Put new value } ((), mapList); } else { ((), ()); } } } } return map; } }
The effects are as follows:
<sites> <site> <name>RUNOOB</name> <url></url> </site> <site> <name>Google</name> <url></url> </site> <site> <name>Facebook</name> <url></url> </site> </sites>
Effect after converting xml to json
{ "sites": { "site": [ { "name": "RUNOOB", "url": "" }, { "name": "Google", "url": "" }, { "name": "Facebook", "url": "" } ] } }
This is the article about using dom4j to implement xml to map and xml to json strings. For more related contents of dom4j xml to map and xml to json, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!