大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> XML专区 -> 怎样快速从一个XML文件中查找信息

怎样快速从一个XML文件中查找信息

时间: 2021-07-31 作者:daque

在搜集期间,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 prejudicebook title: emmabook 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.9736.936.9咱们不妨看到,30.97是books.xml中一切price值的总和,而36.9则是order.xml中一切price值的总和。经过xpah不只不妨赶快搜索消息,并且还不妨对消息举行少许基础的处置。

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.