Quickly search for information from an XML file

Source: Internet
Author: User

In the Internet era, XML files play a role in data storage and transmission. The SOAP protocol communicates information through XML, and the database accesses information through XML files. So how can we quickly obtain the required information from an XML file?

We know that Java's JAXP and Microsoft. net has XML analyzer, Microsoft. net is read-side analysis, while JAXP is read to the memory before analysis (there is also an event mechanism to read). In short, it is not conducive to fast reading. Based on this, Microsoft. NET and JAXP both provide the XPath mechanism to quickly locate the nodes required in the XML file.

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

<? XML version = "1.0"?>

<! -- A fragment of a book store inventory database -->

<Bookstore xmlns: BK = "urn: samples">

<Book genre = "novel" publicationdate = "1997" BK: ISBN = "1-861001-57-8">

<Title> Pride and Prejudice </title>

<Author>

<First-name> Jane </first-name>

<Last-name> Austen </last-name>

</Author>

<Price> 24.95 </price>

</Book>

<Book genre = "novel" publicationdate = "1992" BK: ISBN = "1-861002-30-1">

<Title> The Handmaid's tale </title>

<Author>

<First-name> Margaret </first-name>

<Last-name> Atwood </last-name>

</Author>

<Price> 29.95 </price>

</Book>

<Book genre = "novel" publicationdate = "1991" BK: ISBN = "1-861001-57-6">

<Title> Emma </title>

<Author>

<First-name> Jane </first-name>

<Last-name> Austen </last-name>

</Author>

<Price> 19.95 </price>

</Book>

<Book genre = "novel" publicationdate = "1982" BK: ISBN = "1-861001-45-3">

<Title> Sense and Sensibility </title>

<Author>

<First-name> Jane </first-name>

<Last-name> Austen </last-name>

</Author>

<Price> 19.95 </price>

</Book>

</Bookstore>

If you want to quickly find all titles whose "last-name" is equal to "Austen", you can use the following method:

Xmlreadersample. CS

// Corelib.net/system.xml.#/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 );

}

}

}

Run this program and the result is:

Book title: Pride and Prejudice

Book title: Emma

Book title: Sense and Sensibility

You can see that the search is correct.

Some functions in XPath can also be used to implement simple sorting and simple operations. For example, you can use XPath to summarize data in a database.

For example:

Order. xml

<! -- Represents a customer order -->

<Order>

<Book ISBN = '10-861003-324 '>

<Title> The Handmaid's tale </title>

<Price> 19.95 </price>

</Book>

<Cd isbn = '2-3631-4 '>

<Title> Americana </title>

<Price> 16.95 </price>

</Cd>

</Order>

And: Books. xml

<? XML version = "1.0"?>

<! -- This file represents a fragment of a book store inventory database -->

<Bookstore>

<Book cc = "DD" xmlns: BK = "urn: sample" xmlns: NS = "http://www.Any.com" genre = "autobiography" publicationdate = "1981" ISBN = "1-861003-11-0">

<Title> The Autobiography of Benjamin Franklin </title>

<Ns: Author>

<First-name> Benjamin </first-name>

<Last-name> Franklin </last-name>

</Ns: Author>

<Price> 8.99 </price>

</Book>

<Book genre = "novel" publicationdate = "1967" ISBN = "0-201-63361-2">

<Title> the confidence man </title>

<Author>

<First-name> Herman </first-name>

<Last-name> Melville </last-name>

</Author>

<Price> 11.99 </price>

</Book>

<Book genre = "Philosophy" publicationdate = "1991" ISBN = "1-861001-57-6">

<Title> the gorgias </title>

<Author>

<Name> Plato </Name>

</Author>

<Price> 9.99 </price>

</Book>

</Bookstore>

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

Evaluate. CS

// Corelib.net/system.xml.#/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 ());

}

}

}

Run this program and the result is as follows:

30.97

36.9

36.9

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. You can not only search for information quickly, but also perform some basic processing on the information through xapis.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.