##XPath is a A language for finding information in XML documents. XPath can be used to traverse elements and attributes in XML documents.
XPath is a major element of the W3C XSLT standard, and both XQuery and XPointer are built on top of XPath expressions.
Therefore, an understanding of XPath is the basis for many advanced XML applications. To learn more about XPath, please refer to //m.sbmmt.com/.
Two ways.
I did this test on the android 2.2 system. I don’t know if it will work if it is lower than 2.2.
Let’s talk about the steps of XPath parsing xml in detail: xpathTest.xml andandroid The way dom parses xml is the same as DomTest.xml in
1. Create InputSources
2. Obtain the XPathFactory instance.
3. Use the XPathFactory instance to obtain the XPath instance
4. XPath calls the evaluate() method to obtain the queried NodeList
private void xPathParserXml(){ //获取XPathFactory实例 XPathFactory factory = XPathFactory.newInstance(); //用工程生成XPath实例,解析xml XPath xpath = factory.newXPath(); // try { InputSource source = new InputSource(getResources().getAssets().open("xPathTest.xml")); //第一个参数:需要查询的节点名称,必须要在节点前加“//” //第二个参数:查询的输入源 //第三个参数:返回的类型 // NodeList nodeList = (NodeList) xpath.evaluate("//group", source, XPathConstants.NODESET); // if(nodeList != null && nodeList.getLength() > 0){ // for(int i = 0;i < nodeList.getLength();i++){ // Node node = nodeList.item(i); // //在这也可以得到的子节点 。但是这样不符合xpath的风格。 // NodeList personList = node.getChildNodes(); // Element nodeAttr =(Element)node; // String groupName = nodeAttr.getAttribute("name"); // String num = nodeAttr.getAttribute("num"); // // Log.e("TEST", ""+groupName+" "+num); // } // } // //获取 节点信息 // NodeList personList = (NodeList) xpath.evaluate("//person", source, XPathConstants.NODESET); // if(personList != null && personList.getLength() > 0){ // for(int i = 0;i < personList.getLength();i++){ // Element node = (Element)personList.item(i); // //在这也可以得到 的子节点 和 。 // NodeList childList = node.getChildNodes(); // String groupName = node.getAttribute("name"); // String age = node.getAttribute("age"); // // Log.e("TEST", ""+groupName+" "+age); // } // } //获取 节点信息 NodeList chineseList = (NodeList) xpath.evaluate("//chinese", source, XPathConstants.NODESET); if(chineseList != null && chineseList.getLength() > 0){ for(int i = 0;i < chineseList.getLength();i++){ Node node = chineseList.item(i); String chinese = node.getTextContent(); Log.e("TEST", ""+chinese); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (XPathExpressionException e) { e.printStackTrace(); } }
Note: xpath.evaluate() cannot be called twice and an error will be reported. The reason is unclear. If you know the reason, please leave a message to let us know, thank you.
Some people have asked whether XPath can query large xml files (more than 1M or more). This should be possible in theory, as long as you can Just solve the problem that InputSource can read large-capacity files.
The above is the content of android XPath parsing xml. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!