HTML parsing XML using XPath

Source: Internet
Author: User
Tags xpath

  • Test.xml:
    <?xml version= "1.0" encoding= "iso-8859-1"? ><bookstore><book category= "COOKING" ><title lang= " En ">everyday italian</title><author>giada De laurentiis</author><year>2005</year ><price>30.00</price></book><book category= "Children" ><title lang= "en" >Harry Potter</title><author>j K. rowling</author><year>2005</year><price>29.99< /price></book><book category= "WEB" ><title lang= "en" >xquery Kick start</title><author >james Mcgovern</author><author>per Bothner</author><author>kurt Cagle</author> <author>james Linn</author><author>vaidyanathan nagarajan</author><year>2003</ Year><price>49.99</price></book><book category= "WEB" ><title lang= "en" >Learning Xml</title><author>erik T. RAY&LT;/AUTHOR&GT;&LT;YEAR&GT;2003&LT;/YEAR&GT;&LT;PRICE&GT;39.95&LT;/price></book></bookstore> 
  • Test.html:
    <script type= "Text/javascript" >/* @desc load xhr file @author lee [<[email protected]>] @param file path @ Param Async Synchronous or asynchronous true asynchronous Flase synchronization @return xmldoc loaded content */function Loaddoc (file,async=true) {if (window.    XMLHttpRequest) {//code for ie7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest ();    }else{//code for IE6, IE5 xmlhttp=new activexobject ("Microsoft.XMLHTTP");                if (async = = = True) {Xmlhttp.onreadystatechange = function () {if (Xmlhttp.readystate < 4) {                Load in}else if (xmlhttp.readystate = = 4 && xmlhttp.status = = 200) {//success                Xmldoc=xmlhttp.responsexml;    return xmldoc}else{//Failed}}} xmlhttp.open ("GET", File,async);    Xmlhttp.send ();        if (async = = = False) {xmldoc=xmlhttp.responsexml; return xmldoc}}var xmldoc = Loaddoc (' Test.xml ', false) var path= "/bookstore/book/titLe "//Code for Ieif (window.    ActiveXObject) {var nodes=xmldoc.selectnodes (path);        for (i=0;i<nodes.length;i++) {document.write (nodes[i].childnodes[0].nodevalue);    document.write ("<br/>"); }}//code for Mozilla, Firefox, Opera, Etc.else if (document.implementation &&    document.implementation.createDocument) {var nodes=xmldoc.evaluate (path, xmldoc, NULL, xpathresult.any_type, NULL);    var result=nodes.iteratenext ();        while (result) {document.write (result.childnodes[0].nodevalue);        document.write ("<br/>");    Result=nodes.iteratenext (); }}</script>
  • Output:
    Harry PotterEveryday ItalianLearning XMLXQuery Kick Start
  • HTML parsing XML using XPath

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.