1.xml文件
<?xml version="1.0" encoding="UTF-8"?> <IndexTab RecNum="1"> <Index IndexName="Index1" IndexDesc="索引字段"> <IndexColumnTab RecNum="2"> <IndexColumn SeqNo="0" ColumnName="acct_no"/> <IndexColumn SeqNo="1" ColumnName="acct_name"/> </IndexColumnTab> <IndexScoper><![CDATA[zheshiyigezhi]]></IndexScoper> </Index> </IndexTab>
2.java
package com.test.xml; import java.io.File; import java.util.Iterator; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class Test { public static void main(String[] args) { SAXReader reader = new SAXReader(); File file = new File("C:/Users/maoheng/Desktop/1.xml"); Document document = null; try { document = reader.read(file);//创建documnet对象 } catch (DocumentException e) { e.printStackTrace(); } Element root = document.getRootElement();//得到根节点IndexTab对象 Iterator<?> it = root.elementIterator("Index");//得到根节点下Index节点的迭代器子元素 while(it.hasNext()){//遍历Index Element index = (Element) it.next(); String indexName = index.attributeValue("IndexName");//得到IndexName属性值 String indexDesc = index.attributeValue("IndexDesc"); System.out.println("IndexName:"+indexName); System.out.println("IndexDesc:"+indexDesc); Element indexColumnTab = index.element("IndexColumnTab");//得到IndexColumnTab对象 Iterator<?> itt = indexColumnTab.elementIterator("IndexColumn"); while(itt.hasNext()){//遍历IndexColumn Element indexColumn = (Element) itt.next(); String seqNo = indexColumn.attributeValue("SeqNo"); String columnName = indexColumn.attributeValue("ColumnName"); System.out.println("SeqNo:"+seqNo); System.out.println("ColumnName:"+columnName); } Element indexScoper = index.element("IndexScoper");//获取IndexScoper对象 String indexscoper = indexScoper.getStringValue();//得到zheshiyigezhi System.out.println("IndexScoper:"+indexscoper); } } }