這篇文章主要介紹了快速從一個XML文件中查找信息的方法,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

我們知道,java的JAXP中和Microsoft.Net都有Xml分析器,Microsoft.Net是邊讀邊分析,而JAXP是讀到內(nèi)存中然后才進行分析(還有一種是事件機制去讀),總而言之,是不利于快速讀取。基于此,Microsoft.Net 和JAXP都提供了XPATH機制,來快速定位到XML文件中所需的節(jié)點。
例如有一個XML文件: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>
如果我們想快速查找”last-name”等于”Austen”的所有標題名,可以通過以下方法可以得到:
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);
}
}
}運行這個程序,結果為:
Book title: Pride And Prejudice Book title: Emma Book title: Sense and Sensibility
可以看到查找正確。
利用XPATH中的一些功能,也可以實現(xiàn)簡單的排序和簡單運算。如在數(shù)據(jù)庫中經(jīng)常要對數(shù)據(jù)進行匯總,就可用XPATH實現(xiàn)。
如:
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>
和: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>
我們可以對該XML文件中的price求和,以得到價格總數(shù)。
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());
}
}
}運行這個程序,結果如下:
30.97 36.9 36.9
我們可以看到,30.97是books.xml中所有price值的總和,而36.9則是order.xml中所有price值的總和。通過XPAH不僅可以快速查找信息,而且還可以對信息進行一些基本的處理。
感謝你能夠認真閱讀完這篇文章,希望小編分享快速從一個XML文件中查找信息的方法內(nèi)容對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,遇到問題就找創(chuàng)新互聯(lián),詳細的解決方法等著你來學習!
文章標題:快速從一個XML文件中查找信息的方法-創(chuàng)新互聯(lián)
標題來源:http://www.chinadenli.net/article46/pdoeg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供面包屑導航、App設計、微信小程序、移動網(wǎng)站建設、定制開發(fā)、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)