• dom4j--解析xml文件


    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);
            }
            
        }
    
    }
  • 相关阅读:
    理想解法
    IEEExtreme 2021
    day_1-python前期学习准备篇
    电梯模拟C++
    java线程_01——————————HelloWorld例子
    Unknown tag (c:forEach) 未知的标签
    自动生成Junit单元测试的插件 CodeProAnalytix
    Log4j笔记----01
    Springboot学习笔记_helloWorld篇
    支持开源,崇尚技术,追求真理,充实人生
  • 原文地址:https://www.cnblogs.com/it-mh/p/10455448.html
Copyright © 2020-2023  润新知