A detailed introduction to teach you how to quickly find information from an XML file

黄舟
Release: 2017-03-06 16:35:06
Original
2523 people have browsed it

In the Internet era, xml files play a role in saving and transmitting data. The Soap protocol communicates information through Xml, and the database is accessed through Xml files, etc. So how to quickly obtain the required information from an XML file?

We know that both Java's JAXP and Microsoft.Net have Xml parsers. Microsoft.Net analyzes while reading, while JAXP reads it into the memory and then analyzes it (there is also an event mechanism to read), in short, is not conducive to fast reading. Based on this, both Microsoft.Net and JAXP provide the XPATH mechanism to quickly locate the required nodes in the XML file.

For example, there is an XML file: booksort.xml:

    PRide And Prejudice  Jane Austen  24.95   The Handmaid's Tale  Margaret Atwood  29.95   Emma  Jane Austen  19.95   Sense and Sensibility  Jane Austen  19.95  
Copy after login

If we want to quickly find all title names with "last-name" equal to "Austen", we can get it through the following method:

XmlReaderSample.cs //Corelib.net/System.Xml.Xsl/XPathDocument Class //Author :Any using System; using System.IO; using System.Xml; using System.Xml.XPath; public class XmlReaderSample { public static void Main() { XmlTextReader myxtreader = new XmlTextReader("booksort.xml"); XmlReader myxreader = myxtreader; XPathDocument doc = new XPathDocument(myxreader); XPathNavigator nav = doc.CreateNavigator(); XPathExpression expr; expr = nav.Compile("descendant::book[author/last-name='Austen']"); //expr.AddSort("title", XmlSortOrder.Ascending, XmlCaSEOrder.None, "", XmlDataType.Text); XPathNodeIterator iterator = nav.Select(expr); while (iterator.MoveNext()) { XPathNavigator nav2 = iterator.Current; nav2.MoveToFirstChild(); Console.WriteLine("Book title: {0}", nav2.Value); } } }
Copy after login

Run this program, the result is:

Book title: Pride And Prejudice Book title: Emma Book title: Sense and Sensibility
Copy after login

You can see that the search is correct.

Using some functions in XPATH, simple sorting and simple operations can also be achieved. If you often need to summarize data in a database, you can use XPATH to achieve this.

For example:

order.xml    The Handmaid's Tale 19.95   Americana 16.95  
Copy after login

and: books.xml

    The Autobiography of Benjamin Franklin  Benjamin Franklin  8.99   The Confidence Man  Herman Melville  11.99   The Gorgias  Plato  9.99  
Copy after login

We can sum the prices in the XML file to get the total price.

Evaluate.cs //Corelib.net/System.Xml.Xsl/XPathNavigator Class //Author :Any using System; using System.IO; using System.Xml; using System.Xml.XPath; public class EvaluateSample { public static void Main() { EvaluateSample myEvaluateSample = new EvaluateSample(); myEvaluateSample.test("books.xml"); } public void test(String args) { try { //test Evaluate(String); XPathDocument myXPathDocument = new XPathDocument(args); XPathNavigator myXPathNavigator = myXPathDocument.CreateNavigator(); Console.WriteLine(myXPathNavigator.Evaluate("sum(descendant::book/price)")); //testEvaluate(XPathExpression); XmlDocument doc = new XmlDocument(); doc.Load("order.xml"); XPathNavigator nav = doc.CreateNavigator(); XPathExpression expr = nav.Compile("sum(//price/text())"); Console.WriteLine(nav.Evaluate(expr)); //testEvaluate(XPathExpression); XPathNodeIterator myXPathNodeIterator = nav.Select("descendant::book/title"); expr = nav.Compile("sum(//price/text())"); Console.WriteLine(nav.Evaluate(expr,myXPathNodeIterator)); } catch (Exception e) { Console.WriteLine ("Exception: {0}", e.ToString()); } } }
Copy after login

Run this program, the results are as follows:

30.97 36.9 36.9
Copy after login

We can see that 30.97 is the sum of all price values in books.xml, and 36.9 is the sum of all price values in order.xml sum. Not only can you quickly find information through XPAH, but you can also perform some basic processing on the information.

The above is a detailed introduction to teach you how to quickly find information from an XML file. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!