import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.context.annotation.Bean;
/**
* @ProjectName: springboot-dynamicdb
* @Package: com.item.common.utils
* @ClassName: ParseXmlStrUtil
* @Author: liangsw
* @Description: 类XML文件内容解析工具类
* @Date: 2019/11/14 8:54
* @Version: 1.0
*/
public class ParseXmlStrUtil {
public static HashMap<String, String> stringToXmlByDom4j(String content) {
HashMap<String, String> result = new HashMap<String, String>();
try {
SAXReader saxReader = new SAXReader();
org.dom4j.Document docDom4j = saxReader.read(new ByteArrayInputStream(content.getBytes("utf-8")));
// 获取根节点
org.dom4j.Element root = docDom4j.getRootElement();
// 获取根节点下的属性
List<Attribute> rooAttrList = root.attributes();
// 设置根目录下的属性值
for (Attribute rootAttr : rooAttrList) {
// System.out.println(rootAttr.getName() + ": " + rootAttr.getValue());
result.put(rootAttr.getName(), rootAttr.getValue());
}
// 第二层 获取根目录下的子节点(儿子节点)
List<org.dom4j.Element> childElements = root.elements();
// 设置儿子节点的map值
for (Element element : childElements) {
result.put(element.getName(), element.getText());
}
// 循环儿子节点
for (org.dom4j.Element child : childElements) {
// 未知属性名情况下 获取儿子节点的属性列表
List<Attribute> attributeList = child.attributes();
// 添加儿子节点属性值至Map集合;
for (Attribute attr : attributeList) {
// System.out.println("第二层:" + attr.getName() + ": " + attr.getValue());
result.put(attr.getName(), attr.getValue());
}
// 第三层 获取孙子节点
List<org.dom4j.Element> elementList = child.elements();
for (Element ment : elementList) {
result.put(ment.getName(), ment.getText());
}
for (org.dom4j.Element ele : elementList) {
List<Attribute> kidAttr = ele.attributes();
for (Attribute kidattr : kidAttr) {
// System.out.println("第三层:" + kidattr.getName() + ": " + kidattr.getValue());
result.put(kidattr.getName(), kidattr.getValue());
}
// 第四层
List<org.dom4j.Element> lidList = ele.elements();
for (Element lid : lidList) {
List<Attribute> elemList = lid.attributes();
for (Attribute elem : elemList) {
// System.out.println("第四层:" + elem.getName() + ": " + elem.getValue());
result.put(elem.getName(), elem.getValue());
}
// 第五层
List<org.dom4j.Element> eletList = lid.elements();
int size = eletList.size();
if (size > 0) {
for (org.dom4j.Element elet : eletList) {
// System.out.println("第三层:"+e2.getName() + ": " + e2.getText());
result.put(elet.getName(), elet.getText());
}
}
}
int size = lidList.size();
if (size > 0) {
for (org.dom4j.Element e : lidList) {
// System.out.println("第五层:" + e.getName() + ": " + e.getText());
result.put(e.getName(), e.getText());
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
String xmlStr = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><ns2:requestTokenResponse xmlns:ns2=\"http://coremail.cn/apiws\"><return><code>0</code><result>BAvgzYrrNdlPseOWCArrYYjeZZOMfDct c8a75b26fea7fde7c7d7a3f0a94899d1</result></return></ns2:requestTokenResponse></soap:Body></soap:Envelope>";
HashMap<String, String> mapInfo = stringToXmlByDom4j(xmlStr);
System.out.println(mapInfo);
String value = mapInfo.get("result");
System.out.println("value : " + value);
}
}