• 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正则表达式指南
    Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies(二)
    Python中的urlparse、urllib抓取和解析网页(一)
    __name__ = '__main__'
    odoo context
    Python xlwt模块
    python中使用xlrd、xlwt操作excel
    odoo 下 get_object_reference 函数
    Python运算符
    jQuery实现contains方法不区分大小写的方法教程
  • 原文地址:https://www.cnblogs.com/it-mh/p/10455448.html
Copyright © 2020-2023  润新知