效果图
xml指定的taxML/school/name路径值是:喜洋洋小学
xml指定的taxML/class/student[0]/name路径值是:小明
xml指定的路径taxML/class/student[2]/father/name值是:大亮
xml指定的路径taxML/class/student[2]/father/sons/son[1]/name值是:二亮
Process finished with exit code 0
格式化的xml数据
<?xml version="1.0" encoding="UTF-8"?>
<taxML xmlns="http://www.chinatax.gov.cn/dataspec/">
<class>
<student>
<name>小明</name>
<age>8</age>
<father>
<name>大明</name>
<age>40</age>
</father>
</student>
<student>
<name>小红</name>
<age>8</age>
<father>
<name>大红</name>
<age>42</age>
</father>
</student>
<student>
<name>小亮</name>
<age>8</age>
<father>
<name>大亮</name>
<age>39</age>
<sons>
<son>
<name>小亮</name>
<age>8</age>
</son>
<son>
<name>二亮</name>
<age>10</age>
</son>
</sons>
</father>
</student>
</class>
<school>
<name>喜洋洋小学</name>
<age>8</age>
</school>
</taxML>
需要导入的包
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Document;
自己写的代码
public static void main(String[] args){
// String strXml = FileUtil.readFile("C:\\Users\\kungeek\\Desktop\\test\\school.xml");
String strXml ="<?xml version=\"1.0\" encoding=\"UTF-8\"?><taxML xmlns=\"http://www.chinatax.gov.cn/dataspec/\"><class><student><name>小明</name><age>8</age><father><name>大明</name><age>40</age></father></student><student><name>小红</name><age>8</age><father><name>大红</name><age>42</age></father></student><student><name>小亮</name><age>8</age><father><name>大亮</name><age>39</age><son><name>小亮</name><age>8</age></son><son><name>二亮</name><age>10</age></son></father></student></class><school><name>喜洋洋小学</name><age>8</age></school></taxML>";
String value = xmlpathValue(strXml,"taxML/school/name");
String value2 = xmlpathValue(strXml,"taxML/class/student[0]/name");
String value3 = xmlpathValue(strXml,"taxML/class/student[2]/father/name");
String value4 = xmlpathValue(strXml,"taxML/class/student[2]/father/son[1]/name");
System.out.println("xml指定的值是:"+value);
System.out.println("xml指定的值是:"+value2);
System.out.println("xml指定的值是:"+value3);
System.out.println("xml指定的值是:"+value4);
}
public static String xmlpathValue(String xml ,String xmlPath){
try {
Document document = DocumentHelper.parseText(xml);
String [] eleName = xmlPath.split("/");
List<String> list = new LinkedList<>(Arrays.asList(eleName));
//获取文档根节点
Element root = document.getRootElement();
String rootName = root.getName();
if(rootName.equals(list.get(0))){
list.remove(0);
return getNodeElement(root,list);
}else{
return null;
}
} catch (Exception e) {
return null;
}
}
public static String getNodeElement(Element element,List<String> list) {
//不做为空判断,如果数据格式错误,就应该抛错
String eleName = list.get(0);
if(list.size()>1){
if(eleName.indexOf("[") > -1 && eleName.indexOf("]") >-1){
List<Element> listChild = element.elements();
String eleIndex = eleName.substring(eleName.indexOf("[")+1,eleName.indexOf("]"));
Element child = listChild.get(Integer.parseInt(eleIndex));
list.remove(0);
return getNodeElement(child,list);
}else{
Element child = element.element(eleName);
list.remove(0);
return getNodeElement(child,list);
}
}else{
return element.element(eleName).getText();
}
}