XML 使用 Xpath 来多节点属性匹配查询 具体如下: XML文档 <?xml version="1.0" encoding="utf-8"?>
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了XML 使用 Xpath 来多节点属性匹配查询 具体如下: XML文档 <?xml version="1.0" encoding="utf-8"?>相关的知识,希望对你有一定的参考价值。
<?xml version="1.0" encoding="utf-8"?>
<!-- 订单表相关配置 -->
<MIC key="订单表相关配置">
<!-- 订单状态 -->
<OrderStatus key="订单状态" keyIsOnly="1" value="value值" valueIsOnly="1">
<Item key="客服未审核" value="0">
<Item key="客服未审核,初始订单" value="0" />
</Item>
<Item key="客服已审核" value="1">
<Item key="客服已审核,取消订单" value="1" />
<Item key="客服已审核,未付款" value="2" />
</Item>
</OrderStatus >
</MIC>
需求如下:
使用Xpath 获取OrderStatus 节点下Item value等于0或者1的数据
注:
/MIC/OrderStatus/Item[@value=1 or @value=0]方法无效.
/MIC/OrderStatus/Item[@value=5] | Item[@value=3]方法无效.
求解.
用DOM解析XML ,用xpath快速查询XML节点
XPath是一种快速查询xml节点和属性的一种语言,Xpath和xml的关系就像是sql语句和数据库的关系。用sql语句可以从数据库中快速查询出东西同样的用xPath也可以快速的从xml中查询出东西。
下面的示例演示了怎么用jdk自带的rt.jar完成dom解析
代码如下:
test.xml的代码如下:
<?xml version="1.0" encoding="UTF-8" ?> <inventory> <book year="2012"> <title>菜根谭</title> <author>洪应明</author> <dynasty>明朝</dynasty> <price>38</price> </book> <book year="2013"> <title>曾国藩家书</title> <author>曾国藩</author> <dynasty>清朝</dynasty> <price>89</price> </book> <book year="2014"> <title>高等代数</title> <author>丘维声</author> <dynasty>中华人民共和国</dynasty> <price>86</price> </book> </inventory>
ParseXmlTest的代码如下:
package com.timo.xml; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.xpath.*; import java.io.File; /** * @author Abraham Qin * @since 2018/11/11 */ public class ParseXmlTest {
//查询价格大于40的书的标题的内容 private static final String XPath_EXPRESSION = "//book[price>40]/title/text()"; public static void main(String[] args) throws Exception { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); //开启验证: documentBuilderFactory.setValidating(true); documentBuilderFactory.setNamespaceAware(false); documentBuilderFactory.setIgnoringComments(true); documentBuilderFactory.setIgnoringElementContentWhitespace(true); documentBuilderFactory.setCoalescing(false); documentBuilderFactory.setExpandEntityReferences(true); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); //设置异常处理: documentBuilder.setErrorHandler(new ErrorHandler() { @Override public void warning(SAXParseException exception) throws SAXException { System.out.println("warn:" + exception.getMessage()); } @Override public void error(SAXParseException exception) throws SAXException { System.out.println("error:" + exception.getMessage()); } @Override public void fatalError(SAXParseException exception) throws SAXException { System.out.println("fatalError:" + exception.getMessage()); } }); //将test.xml加载到一个Document的对象中: String filePath = "E:\mybatis-indepth-research\mybatis-3\src\main\java\com\timo\xml\test.xml"; Document document = documentBuilder.parse(new File(filePath)); //查询author为洪应明的title: processParseXmlWithXpath(document,XPath_EXPRESSION ); } private static void processParseXmlWithXpath(Document document, String xPathExpression) throws Exception { //创建XPathFactory: XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xPath = xPathFactory.newXPath(); XPathExpression expression = xPath.compile(xPathExpression); Object result = expression.evaluate(document, XPathConstants.NODESET); if (result instanceof NodeList) { NodeList nodes = (NodeList) result; for (int i = 0; i < nodes.getLength(); i++) { System.out.println(String.format("%s=%s", nodes.item(i).getNodeName(), nodes.item(i).getNodeValue())); } } } }
以上是关于XML 使用 Xpath 来多节点属性匹配查询 具体如下: XML文档 <?xml version="1.0" encoding="utf-8"?>的主要内容,如果未能解决你的问题,请参考以下文章