zoukankan      html  css  js  c++  java
  • 【记录】java解析xml文件

      最近新需求要解析xml格式的日志文件,解析完之后数据库落地。

    经过度娘搜索,写了demo,现记录如下:

     测试XML

    <?xml version="1.0" encoding="UTF-8"?>
    <stus core-uuid = "4bc7e7f6-14db-11ea-b8e5-9fa46b26ed54" switchname="iZbp1i0fg2swiwfhc4zzqnZ">
        <stu id="10086">
            <name>张三</name>
            <age>18</age>
            <address>深圳</address>
        </stu>
        <stu id="10086">
            <name>李四</name>
            <name>李五</name>
            <name>李六</name>
            <age>28</age>
            <address>北京</address>
        </stu>
    </stus>
    

      

    JAVA类

    public class Test{
    
    private static  String FilePath = "D:/NewFile.xml";
    
    public static void analysisXml() {
            //1. 创建sax读取对象
            SAXReader saxReader = new SAXReader();
            //2. 指定解析的xml源
            Document document = null;
         // 通过reader对象的read方法加载books.xml文件,获取docuemnt对象。
    //        Document document = reader.read(new File("src/res/books.xml"));
            try {
    //            document = saxReader.read(new URL("https://www.w3school.com.cn/example/xmle/note.xml"));
    //            document = saxReader.read(new ByteArrayInputStream(FilePath.getBytes("UTF-8")));
                document = saxReader.read(new File(FilePath));
            } catch (Exception e1) {
                log.error("解析调用结果异常", e1);
                e1.printStackTrace();
            }
            //3. 得到根元素
            Element root = document.getRootElement();
            System.out.println("=====父节点=====");
            System.out.println("rootgetName--"+root.getName());
            System.out.println("rootattributeValue--"+root.attributeValue("core-uuid"));
            System.out.println("rootattributeValue--"+root.attributeValue("switchname"));
            System.out.println("rootgetPath--"+root.getPath());
            System.out.println("rootgetNamespace--"+root.getNamespace());
            System.out.println("roottoString--"+root.toString());
            System.out.println("=====结束=====");
         // 通过element对象的elementIterator方法获取迭代器
            Iterator it = root.elementIterator();
            // 遍历迭代器,获取根节点中的信息(书籍)
            while (it.hasNext()) {
                System.out.println("=====开始遍历=====");
                Element element = (Element) it.next();
               System.out.println("elementName::"+element.getName()); 
                // 获取book的属性名以及 属性值
                List<Attribute> attrs = element.attributes();
                for (Attribute attr : attrs) {
                    System.out.println("属性名:" + attr.getName() + "--属性值:"
                            + attr.getValue());
                }
                Iterator itt = element.elementIterator();
                while (itt.hasNext()) {
                    Element bookChild = (Element) itt.next();
                    System.out.println("节点名:" + bookChild.getName() + "--节点值:" + bookChild.getStringValue());
                }
                System.out.println("=====结束遍历=====");
            }
        }
        
        public static void main(String[] args) {
            analysisXml();
        }
    }
    

      

    引用包

    import java.io.File;
    import java.util.Iterator;
    import java.util.List;
    import org.dom4j.Attribute;
    import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    

      

    输出结果

    =====父节点=====
    rootgetName--stus
    rootattributeValue--4bc7e7f6-14db-11ea-b8e5-9fa46b26ed54
    rootattributeValue--iZbp1i0fg2swiwfhc4zzqnZ
    rootgetPath--/stus
    rootgetNamespace--org.dom4j.Namespace@babe [Namespace: prefix  mapped to URI ""]
    roottoString--org.dom4j.tree.DefaultElement@1700915 [Element: <stus attributes: [org.dom4j.tree.DefaultAttribute@21de60b4 [Attribute: name core-uuid value "4bc7e7f6-14db-11ea-b8e5-9fa46b26ed54"], org.dom4j.tree.DefaultAttribute@c267ef4 [Attribute: name switchname value "iZbp1i0fg2swiwfhc4zzqnZ"]]/>]
    =====结束=====
    =====开始遍历=====
    elementName::stu
    属性名:id--属性值:10086
    节点名:name--节点值:张三
    节点名:age--节点值:18
    节点名:address--节点值:深圳
    =====结束遍历=====
    =====开始遍历=====
    elementName::stu
    属性名:id--属性值:10086
    节点名:name--节点值:李四
    节点名:name--节点值:李五
    节点名:name--节点值:李六
    节点名:age--节点值:28
    节点名:address--节点值:北京
    =====结束遍历=====
    

      

    参考地址:https://blog.csdn.net/myme95/article/details/94454324

    参考地址:https://www.cnblogs.com/bingru/p/10438020.html

    参考地址:https://www.cnblogs.com/zhuawang/p/3371291.html

    参考地址:https://blog.csdn.net/testTop/article/details/93527796

  • 相关阅读:
    新概念 Lesson 2 Sorry, sir.
    记一次无法正常本地登陆Linux服务器(确定密码正确)
    English trip -- VC(情景课)5 D
    新概念 Lesson 1 Excuse me!
    English trip -- VC(情景课)5 C It's on Main Street 在主街上
    English trip -- Iris老师整理的一般时态
    English trip -- VC(情景课)5 B Places around town 城市设施
    English trip -- VC(情景课)5 Around Town
    Ploya定理学习笔记
    莫比乌斯反演学习笔记
  • 原文地址:https://www.cnblogs.com/wbl001/p/11983871.html
Copyright © 2011-2022 走看看