欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用dom4j解析xml示例

程序员文章站 2022-06-20 10:57:19
...

使用dom4j解析xml示例

xml内容

<?xml version="1.0" encoding="utf-8"?>
<nagios>
    <status>success</status>
    <host_list>
        <host ip="10.1.1.103" online="up">
            <service name="linux-cpu" status="critical" last_check="06-30-2020 17:45:57">
                <![CDATA[ (CPU OK : user=0% system=0% iowait=0% idle=100%) ]]></service>
            <service name="linux-disk" status="ok" last_check="06-30-2020 17:49:17">
                <![CDATA[ DISK OK - free space: / 26615 MB (71.52% inode=97%): ]]></service>
            <service name="linux-mem" status="critical" last_check="06-30-2020 01:43:09">
                <![CDATA[ (CPU OK : user=0% system=0% iowait=0% idle=100%) ]]></service>
        </host>
        <host ip="10.1.1.150" online="up">
            <service name="linux-cpu" status="ok" last_check="06-30-2020 17:46:47">
                <![CDATA[ CPU OK : user=0% system=0% iowait=0% idle=100% ]]></service>
            <service name="linux-disk" status="ok" last_check="06-30-2020 17:50:07">
                <![CDATA[ DISK OK - free space: / 33521 MB (90.08% inode=97%): ]]></service>
            <service name="linux-mem" status="critical" last_check="06-30-2020 01:43:59">
                <![CDATA[ Connection refused or timed out ]]></service>
        </host>
        <host ip="10.1.1.155" online="down">
            <service name="linux-cpu" status="critical" last_check="06-30-2020 17:47:37">
                <![CDATA[ Connection refused or timed out ]]></service>
            <service name="linux-disk" status="critical" last_check="06-30-2020 17:50:57">
                <![CDATA[ Connection refused or timed out ]]></service>
            <service name="linux-mem" status="critical" last_check="06-30-2020 01:44:49">
                <![CDATA[ Connection refused or timed out ]]></service>
        </host>
        <host ip="10.1.1.156" online="up">
            <service name="linux-cpu" status="ok" last_check="06-30-2020 17:48:27">
                <![CDATA[ CPU OK : user=0% system=0% iowait=0% idle=100% ]]></service>
            <service name="linux-disk" status="ok" last_check="06-30-2020 01:52:19">
                <![CDATA[ DISK OK - free space: / 425444 MB (95.70% inode=99%): ]]></service>
            <service name="linux-mem" status="ok" last_check="06-30-2020 01:45:39">
                <![CDATA[ OK - 3.5% (139516 kB) used. ]]></service>
        </host>
    </host_list>
</nagios>

使用java进行硬性解析
1.在pom.xml文件中添加dom4j依赖

<!--dom4j xml解析-->
 <dependency>
   <groupId>dom4j</groupId>
   <artifactId>dom4j-core</artifactId>
   <version>1.4-dev-8</version>
 </dependency>

2.java解析示例代码

public static void test1() throws Exception{
        List<Map<String,Object>> result = new ArrayList<>();
        String xmlFilePath = "demo.xml";
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(xmlFilePath);
        Element nagios = document.getRootElement();
        Element status = nagios.element("status");
        String statusText = status.getText();
        if (statusText.equalsIgnoreCase("success")) {
            Element hostList = nagios.element("host_list");
            List<Element> hostLists = hostList.elements();
            for (Element host : hostLists) {
                Map<String,Object> hostMap = new HashMap<>();
                hostMap.put("ip",host.attributeValue("ip"));
                hostMap.put("online",host.attributeValue("online"));
                List<Map<String,Object>> data = new ArrayList<>();
                /*System.out.println("------开始------");
                System.out.println("ip:"+host.attributeValue("ip")+",online:"+host.attributeValue("online"));*/
                List<Element> services  = host.elements();
                for (Element service : services) {
                    Map<String,Object> obj = new HashMap<>();
                    obj.put("name",service.attributeValue("name"));
                    obj.put("status",service.attributeValue("status"));
                    obj.put("last_check",service.attributeValue("last_check"));
                    obj.put("content",service.getText().trim());
                    data.add(obj);
                    /*System.out.println("name:"+service.attributeValue("name"));
                    System.out.println("status:"+service.attributeValue("status"));
                    System.out.println("last_check:"+service.attributeValue("last_check"));
                    System.out.println("data:"+service.getText().trim());*/
                }
                hostMap.put("data",data);
                result.add(hostMap);
                /*System.out.println("-------结束----------");*/
            }
        }
        String s = JSONArray.fromObject(result).toString();
        System.out.println(s);
    }

3.输出示例json串

[
    {
        "data":[
            {
                "name":"linux-cpu",
                "last_check":"06-30-2020 17:45:57",
                "content":"(CPU OK : user=0% system=0% iowait=0% idle=100%)",
                "status":"critical"
            },
            Object{...},
            Object{...}
        ],
        "ip":"10.1.1.103",
        "online":"up"
    },
    {
        "data":Array[3],
        "ip":"10.1.1.150",
        "online":"up"
    },
    {
        "data":Array[3],
        "ip":"10.1.1.155",
        "online":"down"
    },
    {
        "data":Array[3],
        "ip":"10.1.1.156",
        "online":"up"
    }
]