佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > 怎樣快速從一個(gè)XML文件中查找信息

怎樣快速從一個(gè)XML文件中查找信息

熱門標(biāo)簽:美圖手機(jī) 鐵路電話系統(tǒng) 服務(wù)器配置 智能手機(jī) 檢查注冊表項(xiàng) 呼叫中心市場需求 銀行業(yè)務(wù) 網(wǎng)站文章發(fā)布
在網(wǎng)絡(luò)時(shí)代,XML文件起到了一個(gè)保存和傳輸數(shù)據(jù)的作用。Soap協(xié)議通過Xml交流信息,數(shù)據(jù)庫通過Xml文件存取等等。那么怎樣快速的從一個(gè)XML文件中取得所需的信息呢?

我們知道,JAVA的JAXP中和Microsoft.Net都有Xml分析器,Microsoft.Net是邊讀邊分析,而JAXP是讀到內(nèi)存中然后才進(jìn)行分析(還有一種是事件機(jī)制去讀),總而言之,是不利于快速讀取?;诖耍琈icrosoft.Net 和JAXP都提供了XPATH機(jī)制,來快速定位到XML文件中所需的節(jié)點(diǎn)。

例如有一個(gè)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”的所有標(biāo)題名,可以通過以下方法可以得到:

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);

}

}

}

運(yùn)行這個(gè)程序,結(jié)果為:

Book title: Pride And Prejudice

Book title: Emma

Book title: Sense and Sensibility


可以看到查找正確。

利用XPATH中的一些功能,也可以實(shí)現(xiàn)簡單的排序和簡單運(yùn)算。如在數(shù)據(jù)庫中經(jīng)常要對數(shù)據(jù)進(jìn)行匯總,就可用XPATH實(shí)現(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求和,以得到價(jià)格總數(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());

}

}


}

運(yùn)行這個(gè)程序,結(jié)果如下:

30.97

36.9

36.9

我們可以看到,30.97是books.xml中所有price值的總和,而36.9則是order.xml中所有price值的總和。通過XPAH不僅可以快速查找信息,而且還可以對信息進(jìn)行一些基本的處理。

標(biāo)簽:河南 新疆 沈陽 紅河 滄州 長治 樂山 上海

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《怎樣快速從一個(gè)XML文件中查找信息》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    高尔夫| 沧源| 丘北县| 墨脱县| 云浮市| 涟源市| 万安县| 吉林省| 大渡口区| 无极县| 西林县| 商都县| 英德市| 江阴市| 邓州市| 彭山县| 营口市| 横山县| 开远市| 泽库县| 金坛市| 广安市| 大姚县| 河北区| 武清区| 涞水县| 乡城县| 龙川县| 桃源县| 平度市| 贡山| 秭归县| 珲春市| 嘉荫县| 嘉义县| 南澳县| 新蔡县| 永兴县| 扶绥县| 宜州市| 儋州市|