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