Quickly find information from an XML file

xiaoxiao2021-03-06  102

In the network era, the XML file has played a role of saving and transferring data. The SOAP protocol communicates information through XML, the database passes XML file access, and more. So how do you get the required information from an XML file?

We know that there is an XML analyzer in Java's JAXP and Microsoft.net, and Microsoft.net is a side-read analysis, while JAXP is read in memory and then analyzes (there is an event mechanism to read), in all, in all, It is not conducive to quick reading. Based on this, all Microsoft.Net and JAXP provide XPath mechanism to quickly locate the nodes required in the XML file.

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

pride and prejudice </ title></p> <p><author></p> <p><first-name> jane </ first-name></p> <p><last-name> austen </ last-name></p> <p></ author></p> <p><price> 24.95 </ price></p> <p></ book></p> <p><book genre = "novel" publicationdate = "1992" BK: ISBN = "1-861002-30-1"></p> <p><title> The handmaid's tale </ title></p> <p><author></p> <p><first-name> margaret </ first-name></p> <p><Last-Name> Atwood </ last-name></p> <p></ author></p> <p><price> 29.95 </ price></p> <p></ book></p> <p><book genre = "novel" publicationdate = "1991" BK: ISBN = "1-861001-57-6"></p> <p><title> Emma </ Title></p> <p><author></p> <p><first-name> jane </ first-name></p> <p><last-name> austen </ last-name></p> <p></ author></p> <p><price> 19.95 </ price></p> <p></ book></p> <p><book genre = "novel" publicationdate = "1982" BK: ISBN = "1-861001-45-3"></p> <p><title> Sense and Sensibility </ Title></p> <p><author></p> <p><first-name> jane </ first-name></p> <p><last-name> austen </ last-name></p> <p></ author> <price> 19.95 </ price></p> <p></ book></p> <p></ bookstore></p> <p>If we want to quickly find all the title names equal to "AUSTEN", you can get:</p> <p>XmlReadersample.cs</p> <p>//Corelib.Net/system.xml.xsl/xpathdocument class</p> <p>// Author: Any</p> <p>Using system;</p> <p>Using system.io;</p> <p>USING SYSTEM.XML;</p> <p>Using system.xml.xpath;</p> <p>Public Class XmlReadersample</p> <p>{</p> <p>Public static void main ()</p> <p>{</p> <p>XmlTextReader myXTReader = New XMLTextReader ("Booksort.xml");</p> <p>XmlReader myxreader = myxtreader;</p> <p>XpathDocument Doc = New XpathDocument (MyXReader);</p> <p>Xpathnavigator NAV = doc.createnavigator ();</p> <p>XPATHEXPRESSION EXPR;</p> <p>Expr = Nav.Compile ("Descendant :: Book [Author / Last-Name = 'Austen');</p> <p>//expr.addsort ("teple ", xmlsortorder.ascending, xmlcaseorder.none," ", xmldattype.text);</p> <p>XpathnodeEiterator itrator = nav.select (expr);</p> <p>While (item.movenext ())</p> <p>{</p> <p>Xpathnavigator nav2 = item.current;</p> <p>Nav2.movetofirstchild ();</p> <p>Console.writeline ("Book Title: {0}", Nav2.Value);</p> <p>}</p> <p>}</p> <p>}</p> <p>Run this program, the result is:</p> <p>Book Title: Pride and Prejudice</p> <p>Book Title: Emma</p> <p>Book Title: Sense and Sensibility</p> <p>You can see the looks correct.</p> <p>Simple sorting and simple operations can also be achieved using some of the features in XPath. If you want to summarize the data in the database, you can implement Xpath.</p> <p>Such as:</p> <p>ORDER.XML</p> <p><! - represents a readyOMer Order -></p> <p><Order></p> <p><book isbn = '10 -861003-324 '></p> <p><title> The handmaid's tale </ title></p> <p><price> 19.95 </ price></p> <p></ book></p> <p><CD ISBN = '2-3631-4'></p> <p><title> Americana </ title></p> <p><price> 16.95 </ price></p> <p></ cd></p> <p></ order></p> <p>And: books.xml</p> <p><? XML Version = "1.0"?></p> <p><! file represents a fragment of a book store inventory data -></p> <p><bookstore> <book cc = "dd" xmlns: bk = "urn: sample" xmlns: ns = "http://www.any.com" genre = "autobiography" publicationDate = "1981" ISBN = "1-861003 -11-0></p> <p><title> The autobiography of benjamin franklin </ title></p> <p><ns: author></p> <p><first-name> Benjamin </ first-name></p> <p><last-name> franklin </ last-name></p> <p></ ns: author></p> <p><price> 8.99 </ price></p> <p></ book></p> <p><book genre = "novel" publicationdate = "1967" ISBN = "0-201-63361-2"></p> <p><title> The confidence man </ title></p> <p><author></p> <p><first-name> Herman </ first-name></p> <p><Last-Name> Melville </ last-name></p> <p></ author></p> <p><price> 11.99 </ price></p> <p></ book></p> <p><book genre = "philosophy" publicationdate = "1991" ISBN = "1-861001-57-6"></p> <p><title> The gorgias </ title></p> <p><author></p> <p><name> plato </ name></p> <p></ author></p> <p><price> 9.99 </ price></p> <p></ book></p> <p></ bookstore></p> <p>We can sum up the price in the XML file to get the total price.</p> <p>Evaluate.cs</p> <p>//Corelib.Net/system.xml.xsl/xpathnavigator class</p> <p>// Author: Any</p> <p>Using system;</p> <p>Using system.io;</p> <p>USING SYSTEM.XML;</p> <p>Using system.xml.xpath;</p> <p>Public Class Evaluatesample</p> <p>{</p> <p>Public static void main ()</p> <p>{</p> <p>EvaluateSample Myevaluatesample = new evataSample ();</p> <p>myevaluateSample.test ("Books.xml");</p> <p>}</p> <p>Public Void Test (String Args)</p> <p>{</p> <p>Try</p> <p>{</p> <p>// Test Evaluate (String);</p> <p>XpathDocument MyXPathDocument = New XPathDocument (args);</p> <p>Xpathnavigator myxpathnavigator = myxpathdocument.createnavigator ();</p> <p>Console.writeline (MyXPathnavigator.evaluate ("Sum (Descendant :: Book / Price)))</p> <p>// Testevaluate (XPATHEXPRESSION); xmlDocument doc = new xmldocument ();</p> <p>Doc.Load ("Order.xml");</p> <p>Xpathnavigator NAV = doc.createnavigator ();</p> <p>XPATHEXPRESSION EXPR = Nav.Compile ("SUM (// Price / text ()")</p> <p>Console.writeline (Nav.Evaluate (expr));</p> <p>// Testevaluate (XPATHEXPRESSION);</p> <p>XpathnodeEiterator = nav.select ("Descendant :: Book / Title);</p> <p>Expr = nav.compile ("SUM (// price / text ()")</p> <p>Console.writeline (Nav.Evaluate (expr, myxpathnodeiterator);</p> <p>}</p> <p>Catch (Exception E)</p> <p>{</p> <p>Console.writeline ("Exception: {0}", E.TOSTRING ());</p> <p>}</p> <p>}</p> <p>}</p> <p>Run this program, the result is as follows:</p> <p>30.97</p> <p>36.9</p> <p>36.9</p> <p>We can see that 30.97 is the sum of all the Price values ​​in Books.xml, and 36.9 is the sum of all equal values ​​in ORDER.XML. The information can not only quickly look for information through XPAH, but also some basic processing.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-122770.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="122770" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.060</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'XQcgGJyCz6ImyqafEntamjE548TMDkISyJOdkq3L2LVVqvsM4HzXQQxzLjsBOmEKy7C6yAYK7QzKDIzF'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>