android XPath parsing xml
XPath is 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 http://www.w3school.com.cn/xpath/index.asp.
XPath is only suitable for querying xml information. It is not recommended to use this method for complete parsing of xml files. The best ways to parse xml files are sax and pull.
I did this test on 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 is the same as DomTest.xml in the android dom parsing xml method
1. Create InputSources
2. Get an XPathFactory instance.
3. Use the XPathFactory instance to get the XPath instance
4. XPath calls the evaluate() method to get 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); // //在这也可以得到<group>的子节点<person>。但是这样不符合xpath的风格。 // NodeList personList = node.getChildNodes(); // Element nodeAttr =(Element)node; // String groupName = nodeAttr.getAttribute("name"); // String num = nodeAttr.getAttribute("num"); // // Log.e("TEST", ""+groupName+" "+num); // } // } // //获取<person>节点信息 // 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); // //在这也可以得到<person>的子节点<chinese>和<english>。 // NodeList childList = node.getChildNodes(); // String groupName = node.getAttribute("name"); // String age = node.getAttribute("age"); // // Log.e("TEST", ""+groupName+" "+age); // } // } //获取<chinese>节点信息 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, an error was 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 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 (www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Using Python to merge and deduplicate XML data XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. When processing XML data, sometimes we need to merge multiple XML files into one, or remove duplicate data. This article will introduce how to use Python to implement XML data merging and deduplication, and give corresponding code examples. 1. XML data merging When we have multiple XML files, we need to merge them

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Importing XML data into the database using PHP Introduction: During development, we often need to import external data into the database for further processing and analysis. As a commonly used data exchange format, XML is often used to store and transmit structured data. This article will introduce how to use PHP to import XML data into a database. Step 1: Parse the XML file First, we need to parse the XML file and extract the required data. PHP provides several ways to parse XML, the most commonly used of which is using Simple

Python implements conversion between XML and JSON Introduction: In the daily development process, we often need to convert data between different formats. XML and JSON are common data exchange formats. In Python, we can use various libraries to convert between XML and JSON. This article will introduce several commonly used methods, with code examples. 1. To convert XML to JSON in Python, we can use the xml.etree.ElementTree module

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some

Python parses special characters and escape sequences in XML XML (eXtensibleMarkupLanguage) is a commonly used data exchange format used to transfer and store data between different systems. When processing XML files, you often encounter situations that contain special characters and escape sequences, which may cause parsing errors or misinterpretation of the data. Therefore, when parsing XML files using Python, we need to understand how to handle these special characters and escape sequences. 1. Special characters and
