码迷,mamicode.com
首页 > 其他好文 > 详细

dom4j--解析xml文件

时间:2019-03-01 12:11:41      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:doc   tst   net   com   data   gets   解析xml   trace   div   

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);
        }
        
    }

}

 

dom4j--解析xml文件

标签:doc   tst   net   com   data   gets   解析xml   trace   div   

原文地址:https://www.cnblogs.com/it-mh/p/10455448.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!