每一年都奔走在自己热爱里

没有人是一座孤岛,总有谁爱着你

Java解析XML格式的字符串

1. 解析Xml

1.1 添加项目依赖

<!-- 解析Xml -->
<dependency>
    <groupId>dom4j</groupId>
    <artifactId>dom4j</artifactId>
    <version>1.6.1</version>
</dependency>

1.2 返回的Xml使用字符串换行

String returnMsg = "<?xml version=\"1.0\" encoding=\"gb2312\"?>" +
                "<Result xmlns=\"https:www.youyoushop.work\">" +
                "<row resultcount=\"1\">" +
                "<userId>001</userId>" +
                "<usersName>张三</usersName>" +
                "<userAge>22</userAge>" +
                "</row>" +
                "<row resultcount=\"2\">" +
                "<userId>002</userId>" +
                "<usersName>李四</usersName>" +
                "<userAge>33</userAge>" +
                "</row>" +
                "</Result>";

测试代码

@Test
public void test1() {
    String returnMsg = "<?xml version=\"1.0\" encoding=\"gb2312\"?>" +
        "<Result xmlns=\"https:www.youyoushop.work\">" +
        "<row resultcount=\"1\">" +
        "<userId>001</userId>" +
        "<usersName>张三</usersName>" +
        "<userAge>22</userAge>" +
        "</row>" +
        "<row resultcount=\"2\">" +
        "<userId>002</userId>" +
        "<usersName>李四</usersName>" +
        "<userAge>33</userAge>" +
        "</row>" +
        "</Result>";

    try {
        Document doc = DocumentHelper.parseText(returnMsg);
        Element roots = doc.getRootElement();
        //            System.out.println("根节点 = [" + roots.getName() + "]");
        //            System.out.println("内容:" + roots.getText());
        Iterator elements = roots.elementIterator();
        while (elements.hasNext()) {
            Element child = (Element) elements.next();
            //System.out.println("节点名称 = [" + child.getName() + "]"+"节点内容:"+child.getText());
            List subElements = child.elements();

            List<Map<String, Object>> treeMapList = new ArrayList<>();

            for (int i = 0; i < subElements.size(); i++) {
                Map<String, Object> mapData = new HashedMap();
                Element subChild = (Element) subElements.get(i);
                mapData.put(subChild.getName(), subChild.getText());
                treeMapList.add(mapData);
                //System.out.println("子节点名称:"+subChild.getName()+";子节点内容:"+subChild.getText());
            }
            System.out.println(treeMapList);
        }
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

测试结果

1.3 返回的Xml没有换行

String strXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <Notification xmlns=\"https://netdist.youyoushop.work\"> <userId>7474741</userId> <userName>不服?咋滴</userName> <userAge>88</userAge> <Message>{\"jobId\":\"666888\",\"requestId\":\"639D1D03-1557-4AD7-9AD7-691F02834516\",\"state\":\"Success\"}</Message> </Notification>";

测试代码

@Test
public void test2() {
    String strXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <Notification xmlns=\"https://netdist.youyoushop.work\"> <userId>7474741</userId> <userName>不服?咋滴</userName> <userAge>88</userAge> <Message>{\"jobId\":\"666888\",\"requestId\":\"639D1D03-1557-4AD7-9AD7-691F02834516\",\"state\":\"Success\"}</Message> </Notification>";

    try {
        Document doc = DocumentHelper.parseText(strXML);

        // 指向根节点
        Element root = doc.getRootElement();

        Iterator it = root.elementIterator();
        while (it.hasNext()) {
            // 一个Item节点
            Element element = (Element) it.next();
            System.out.println(element.getName() + " : " + element.getTextTrim());
        }

    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

测试结果

 

posted @ 2022-04-14 17:06  helloliyh  阅读(4228)  评论(0编辑  收藏  举报