Java 使用 XPath表达式定位节点读取XML方法

 /**
     * 加载配置文件节点
     *
     * @param attributeValue 节点属性值
     * @param age节点属性值
     * @date 2023-12-08
     */
    public static Map<String,String> getConfigXml(String attributeValue,String age){
        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[@age='"+age+"']");
            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;
    }

自定义XML

<?xml version="1.0" encoding="utf-8"?>
<body>
    <project val="Employee">
        <item age="99">
            <dsecribe>人员数量</dsecribe>
            <count>5</count>
        </item>
        <item age="100">
            <dsecribe>人员数量</dsecribe>
            <count>5</count>
        </item>
    </project>

<!--这个节点要通过改变定位表达式来获取-->
    <project val="AreaRelevance">
        <item>
            <regionArea>72</regionArea>
            <SArea>31,32,33,34,96</SArea>
        </item>
    </project>
</body>
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我的炸串拌饼店

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值