【JAVA】Java 使用 XPath表达式定位节点读取自定义XML方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
     * 加载配置文件节点
     * @param attributeValue 节点属性值
     * @param areaCode       节点属性值
     */
    public static Map<String,String> getConfigXml(String attributeValue,String areaCode){
        String filePath="config.xml";
        Map<String,String> resultMap=new HashMap<>();
        try{
            File xmlFile=new File(filePath);
            // 创建DocumentBuilderFactory和DocumentBuilder
            DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
            // 使用DocumentBuilder解析XML文件
            Document doc=dBuilder.parse(xmlFile);
            // 创建XPath对象
            XPathFactory xPathFactory=XPathFactory.newInstance();
            XPath xpath=xPathFactory.newXPath();
            // 使用XPath 表达式定位节点
            XPathExpression expr=xpath.compile("//project[@val='"+attributeValue+"']/item[@areacode='"+areaCode+"']");
            resultMap=parseItemsWithAttribute(doc,expr);
            System.out.println(resultMap);
            System.out.println("count: "+resultMap.get("count"));
        }catch(Exception e){
            e.printStackTrace();
            throw new ServiceException("getConfigXml方法异常");
        }
        return resultMap;
    }
 
     * 加载xml,并返回map集合
     *
     * @param doc  解析的xml
     * @param expr 节点定位表达式
     * @author wyj
     * @date 2023-12-08
     */
    private static Map<String,String> parseItemsWithAttribute(Document doc,XPathExpression expr){
        Map<String,String> resultMap=new HashMap<>();
        try{
 
            // 获取匹配的节点
            Node itemNodes=(Node)expr.evaluate(doc,XPathConstants.NODE);
            //获取节点下的子节点
            NodeList childNodes=itemNodes.getChildNodes();
            // 遍历节点列表并获取子节点的值
            for(int i=0;i<childNodes.getLength();i++){
                Node childNode=childNodes.item(i);
                if(childNode.getNodeType()==Node.ELEMENT_NODE){
                    String childNodeName=childNode.getNodeName();
                    String childNodeValue=childNode.getTextContent();
                    resultMap.put(childNodeName,childNodeValue);
                }
            }
        }catch(XPathExpressionException e){
            e.printStackTrace();
            throw new ServiceException("parseItemsWithAttribute方法异常");
        }
        return resultMap;
    }
 
<br> 自定义XML
<?xml version="1.0" encoding="utf-8"?>
<body>
    <project val="one">
        <item areacode="72">
            <dsecribe>测试</dsecribe>
            <count>5</count>
        </item>
        <item areacode="42">
            <dsecribe>测试2</dsecribe>
            <count>5</count>
        </item>
    </project>
 
<!--这个节点要通过改变定位表达式来获取-->
    <project val="AreaRelevance">
        <item>
            <regionArea>72</regionArea>
            <ShengArea>31,32,33,34,35,36</ShengArea>
        </item>
    </project>
</body>

  

posted @   喝了烫嘴的水  阅读(160)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示