public static void main(String[] args) throws Exception {
/*<?xml version="1.0" encoding="UTF-8"?>
<dataset code="T_MD_HR_EMPLOYEE" name="人员" action="UPDATE" msgid="82228D1D94A64385958918B1BB98B620" createtime="2019-06-27 22:04:54" count="1">
<data>
<EMPLOYEE_NAME>刘娟</EMPLOYEE_NAME>
<USERNUMBER>2036000151000068</USERNUMBER>
<EMPLOYEE_CODE type="pk">2036000151000068</EMPLOYEE_CODE>
<ORG_CODE refdesc="赣州卷烟厂">1010</ORG_CODE>
<DEPARTMENT_CODE refdesc="群团工作科">203600015547</DEPARTMENT_CODE>
<SEX>2</SEX>
<CRDNUM>360122198412210023</CRDNUM>
<REMARK/>
<opResult notes='001'>
<item>
<row>
<cu1 note='002'>115344</cu1>
<cu2>115344</cu2>
</row>
<row>
<cu1 note='003'>11533781</cu1>
<cu2>11533781</cu2>
</row>
</item>
</opResult>
</data>
</dataset>*/
SAXReader reader = new SAXReader();
File file = new File("C:/data_10k.xml");
Document document = reader.read(file);
Element root = document.getRootElement();
//1.解析所有的标签名和标签值root.elementIterator():<EMPLOYEE_NAME>刘娟</EMPLOYEE_NAME>
for (Iterator iter = root.elementIterator(); iter.hasNext();) {
Element element = (Element) iter.next(); // 获取标签对象
for (Iterator iterInner = element.elementIterator(); iterInner.hasNext();) {
// 获取标签对象
Element elementOption = (Element) iterInner.next();
// 获取该标签对象的名称
String tagName = elementOption.getName();
// 获取该标签对象的内容
String tagContent = elementOption.getTextTrim();
//System.out.println("tagName="+tagName+" tagContent="+tagContent);
}
}
//2.解析root标签中的属性名和属性值(解析那级标签的属性值就用那级标签调用.attributeIterator()):<DEPARTMENT_CODE refdesc="群团工作科">
for(Iterator attrIter = root.attributeIterator(); attrIter.hasNext();){
Attribute attribute = (Attribute) attrIter.next();
if (null != attribute) {
String attributeName = attribute.getName();//拿到属性名
String attributeValue=attribute.getValue();//拿到属性值
//System.out.println("attributeName="+attributeName+" attributeValue="+attributeValue);
}
}
//3.解析所有标签中的属性名和属性值
for (Iterator iter = root.elementIterator(); iter.hasNext();) {
//2.获取根目录的属性
for(Iterator attrIter = root.attributeIterator(); attrIter.hasNext();){
Attribute attribute = (Attribute) attrIter.next();
if (null != attribute) {
String attributeName = attribute.getName();//拿到属性名
String attributeValue=attribute.getValue();//拿到属性值
//System.out.println("attributeName="+attributeName+" attributeValue="+attributeValue);
}
}
Element element = (Element) iter.next(); // 获取标签对象
for (Iterator iterInner = element.elementIterator(); iterInner.hasNext();) {
//b获取root以下的所有标签中的属性
Element elementOption = (Element) iterInner.next();
for(Iterator attrIter = elementOption.attributeIterator(); attrIter.hasNext();){
Attribute attribute = (Attribute) attrIter.next();
if (null != attribute) {
String attributeName = attribute.getName();//拿到属性名
String attributeValue=attribute.getValue();//拿到属性值
//System.out.println("attributeName="+attributeName+" attributeValue="+attributeValue);
}
}
}
}
//4.解析指定标签值
List selectNodes = document.selectNodes("//dataset/data/ORG_CODE");
Iterator iterator = selectNodes.iterator();
if(iterator.hasNext()){
Element titleElement = (Element) iterator.next();
String tagName = titleElement.getName();// 获取该标签对象的名称
String tagContent = titleElement.getTextTrim();// 获取该标签对象的内容
//System.out.println("tagName="+tagName+" tagContent="+tagContent);
}
//5.解析指定标签的属性值(方式1)
List selectNod= document.selectNodes("//dataset/data/ORG_CODE/@refdesc");
Iterator iter = selectNod.iterator();
if(iter.hasNext()){
Attribute titleAttribute = (Attribute) iter.next();
String attributeName = titleAttribute.getName();//拿到属性名
String attributeValue=titleAttribute.getValue();//拿到属性值
//System.out.println("attributeName="+attributeName+" attributeValue="+attributeValue);
}
//6.解析指定标签中的属性值(方式2)和标签值
List selectNode = document.selectNodes("//dataset/data/DEPARTMENT_CODE");
Iterator it = selectNode.iterator();
if(it.hasNext()){
//a标签值
Element titleElement = (Element) it.next();
String tagName = titleElement.getName();// 获取该标签对象的名称
String tagContent = titleElement.getTextTrim();// 获取该标签对象的内容
//System.out.println("tagName="+tagName+" tagContent="+tagContent);
for(Iterator attrIter = titleElement.attributeIterator(); attrIter.hasNext();){
Attribute attribute = (Attribute) attrIter.next();
if (null != attribute) {
//b属性值
String attributeName = attribute.getName();//拿到属性名
String attributeValue=attribute.getValue();//拿到属性值
//System.out.println("attributeName="+attributeName+" attributeValue="+attributeValue);
}
}
}
//7.解析指定标签的标签值(指定标签下是list,需要判断标签是否有子节点)
List selectNodeList= document.selectNodes("//dataset/data/opResult/item");
Iterator iterList = selectNodeList.iterator();
if(iterList.hasNext()){
Element titleElement = (Element) iterList.next();
//判断此标签是否有子节点
for (Iterator iterInner = titleElement.elementIterator(); iterInner.hasNext();) {
Element elementOption = (Element) iterInner.next();
//判断此子节点是否有子节点(根据具体的xml判断是否还有下一节点)
for (Iterator i = elementOption.elementIterator(); i.hasNext();) {
Element element = (Element) i.next();
String tagName = element.getName();// 获取该标签对象的名称
String tagContent = element.getTextTrim();// 获取该标签对象的内容
System.out.println("tagName="+tagName+" tagContent="+tagContent);
}
}
}
}