JS load and parse XML string code _javascript tips

Source: Internet
Author: User
JS loading XML Document instance
Books.xml
Copy Code code as follows:

<?xml version= "1.0" encoding= "Iso-8859-1"?>
<bookstore>
<book category= "Children" >
<title lang= "en" >www.jb51.net</title>
<author>j K. rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category= "Cooking" >
<title lang= "en" >everyday italian</title>
<author>giada De laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category= "Web" cover= "Paperback" >
<title lang= "en" >learning xml</title>
<author>erik T. ray</author>
<year>2003</year>
<price>39.95</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>
</bookstore>


Loadxml.htm
Copy Code code as follows:

<script>
Try//internet Explorer
{
Xmldoc=new ActiveXObject ("Microsoft.XMLDOM");
}
catch (E)
{
Try//firefox, Mozilla, Opera, etc.
{
Xmldoc=document.implementation.createdocument ("", "", null);
}
catch (E) {alert (e.message)}
}
Try
{
Xmldoc.async=false;
Xmldoc.load ("books.xml");
document.write (Xmldoc.getelementsbytagname ("title") [0].childnodes[0].nodevalue); }
catch (E) {alert (e.message);}
</script>
<body>
</body>

The following is an example of loading and parsing an XML file, but XML is a custom string that is generally used in the background language.
<ptml> <pead> <script type= "Text/javascript" > Function Parsexml () {text= "<note>"; text=text+ "<to>George</to>"; text=text+ "<from>John</from>"; text=text+ "<peading>Reminder</peading>"; text=text+ "<body>don ' t forget the meeting!</body>"; text=text+ "</note>"; Try//internet Explorer {xmldoc=new ActiveXObject ("Microsoft.XMLDOM"); Xmldoc.async= "false"; Xmldoc.loadxml (text); catch (e) {Try//firefox, Mozilla, Opera, etc. {parser=new domparser (); Xmldoc=parser.parsefromstring (text, "Text/xml"); catch (E) {alert (e.message); Return } document.getElementById (' to '). Innerhtml=xmldoc.getelementsbytagname ("to") [0].childnodes[0].nodevalue; document.getElementById ("from"), Innerhtml=xmldoc.getelementsbytagname ("from") [0].childnodes[0].nodevalue; document.getElementById ("message"). Innerhtml=xmldoc.getelementsbytagname ("The Body") [0].childnodes[0].nodevalue; } </script> </pead> <body onload= "Parsexml ()" > <p>w3schools Internal note</p> <p><b>To:</b> < Span id= "to" ></span> <b>From:</b> <span id= "from" ></span> <b>message:</b& Gt <span id= "message" ></span> </p> </body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

If you read the above example, you can look at the following article.
Http://www.jb51.net/article/14604.htm

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.