zoukankan      html  css  js  c++  java
  • 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);
            }
            
        }
    
    }
  • 相关阅读:
    Python
    QinQ 技术解析
    TLS/SSL 协议
    TLS/SSL 协议
    TLS/SSL 协议
    排序算法之基本排序算法(冒泡、插入、选择)
    Spring Boot 学习笔记--手写版
    mysql -- collection一对多查询
    mybatis 批量操作增删改查
    easyUI之datagrid绑定后端返回数据的两种方式
  • 原文地址:https://www.cnblogs.com/it-mh/p/10455448.html
Copyright © 2011-2022 走看看