在网络时代,xml文件起到了一个保存和传输数据的作用。Soap协议通过Xml交流信息,数据库通过Xml文件存取等等。那么怎样快速的从一个XML文件中取得所需的信息呢?
我们知道,java的JAXP中和Microsoft.Net都有Xml分析器,Microsoft.Net是边读边分析,而JAXP是读到内存中然后才进行分析(还有一种是事件机制去读),总而言之,是不利于快速读取。基于此,Microsoft.Net 和JAXP都提供了XPATH机制,来快速定位到XML文件中所需的节点。
例如有一个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中的一些功能,也可以实现简单的排序和简单运算。如在数据库中经常要对数据进行汇总,就可用XPATH实现。
如:
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求和,以得到价格总数。
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文件中查找信息的详细介绍的内容,更多相关内容请关注PHP中文网(www.php.cn)!

RSS文档是基于XML的结构化文件,用于发布和订阅频繁更新的内容。它的主要作用包括:1)自动化内容更新,2)内容聚合,3)提高浏览效率。通过RSSfeed,用户可以订阅并及时获取来自不同来源的最新信息。

RSS的XML结构包括:1.XML声明和RSS版本,2.频道(Channel),3.条目(Item)。这些部分构成了RSS文件的基础,允许用户通过解析XML数据来获取和处理内容信息。

RSSFEEDSUSEXMLTOSYNDICATECONTENT; PARSINGTHEMINVOLVESLOADINGINGINGINGINSSTRUCTURE,andExtractingData.ApplicationsIncludeBuildBuildingNewSagGregatorSaterNewSagGregatorSator andTrackingPodcastepodcastepisodes。

RSS文档的工作原理是通过XML文件发布内容更新,用户通过RSS阅读器订阅并接收通知。1.内容发布者创建并更新RSS文档。2.RSS阅读器定期访问并解析XML文件。3.用户浏览和阅读更新内容。使用示例:订阅TechCrunch的RSS源,只需复制链接到RSS阅读器中即可。

使用XML构建RSSfeed的步骤如下:1.创建根元素并设置版本;2.添加channel元素及其基本信息;3.添加条目(item)元素,包括标题、链接和描述;4.转换XML结构为字符串并输出。通过这些步骤,你可以从零开始创建一个有效的RSSfeed,并通过添加额外的元素如发布日期和作者信息来增强其功能。

创建RSS文档的步骤如下:1.使用XML格式编写,根元素为,包含元素。2.在内添加、、等元素描述频道信息。3.添加元素,每个代表一个内容条目,包含、、、等。4.可选地添加和元素,丰富内容。5.确保XML格式正确,使用在线工具验证,优化性能并保持内容更新。

XML在RSS中的核心作用是提供一种标准化和灵活的数据格式。1.XML的结构和标记语言特性使其适合数据交换和存储。2.RSS利用XML创建标准化格式,方便内容共享。3.XML在RSS中的应用包括定义feed内容的元素,如标题和发布日期。4.优势包括标准化和可扩展性,挑战包括文件冗长和严格语法要求。5.最佳实践包括验证XML有效性、保持简洁、使用CDATA和定期更新。

rssfeedsarexmldocuments usedforcontentAggregation and distribution.totransformthemintoreadableContent:1)parsethethexmlusinglibrarieslibrariesliblarieslikeparserinparserinpython.2)andledifferentifferentrssssssssssssssssssssssssssssssssssssssssssssssersions andpotentionparsingrorS.3)


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

SublimeText3汉化版
中文版,非常好用

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具