com.bixuebihui.generated.utils.XmlUtils Maven / Gradle / Ivy
package com.bixuebihui.generated.utils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import java.util.*;
/**
* @author xwx
*/
public class XmlUtils {
public static Map xmltoMap(String xml, boolean useAttribute) {
try {
Map map = new HashMap();
Document document = DocumentHelper.parseText(xml);
Element nodeElement = document.getRootElement();
List node = nodeElement.elements();
for (Iterator it = node.iterator(); it.hasNext();) {
Element elm = (Element) it.next();
if(useAttribute){
map.put(elm.attributeValue("label"), elm.getText());
}else{
map.put(elm.getName(), elm.getData().toString());
}
}
return map;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static Map xmltoMap(String xml, String subNodeName, boolean useAttribute) {
try {
Map map = new HashMap();
Document document = DocumentHelper.parseText(xml);
Element nodeElement = document.getRootElement();
Element subNodeElement = getSubNode(subNodeName, nodeElement);
List node = subNodeElement.elements();
for (Iterator it = node.iterator(); it.hasNext();) {
Element elm = (Element) it.next();
if(useAttribute){
map.put(elm.attributeValue("label"), elm.getText());
}else{
map.put(elm.getName(), elm.getData().toString());
}
}
return map;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* xml to list xml value1 value2 ......value1 value2 ......
*
* @param xml
* @return
*/
public static List xmltoList(String xml) {
try {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy