JS Operation XML Instance

Source: Internet
Author: User

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
&LT;TITLE&GT;JS Read XML compatible ie/ff/chrome,safari-www.cxybl.com</title>
<style>
Li{list-style:none;}
</style>
<script type= "Text/javascript" >
var Orderdoc;
Get XML file
function Loadxmlfile (xmlFile) {
var xmldom = null;
if (window. ActiveXObject) {
XMLDOM = new ActiveXObject ("Microsoft.XMLDOM");
Xmldom.async=false;
Xmldom.load (XmlFile) xmldom.loadxml (xmlFile);//If you are using an XML string//If you are using an XML file
}else if (document.implementation && document.implementation.createDocument) {
var xmlhttp = new window. XMLHttpRequest ();
Xmlhttp.open ("Get", XmlFile, false);
Xmlhttp.send (NULL);
XMLDOM = Xmlhttp.responsexml;
}else{
XMLDOM = null;
}
return xmldom;
}
To determine that a child node is not empty
function IsNull (obj)
{
var nodevalue = "";
if (Obj.childnodes[0]!= null)
{
NodeValue =obj.childnodes[0].nodevalue;
}
return nodevalue;
}
var stringsss= "";
Get data from a number
function Getdatabyid (number)
{
var time =isnull (Orderdoc.getelementsbytagname ("Time") [number]);
var place =isnull (Orderdoc.getelementsbytagname ("place") [number]);
var reason =isnull (Orderdoc.getelementsbytagname ("Reason") [number]);
var unit = IsNull (orderdoc.getelementsbytagname ("unit") [number]);
var =isnull (Orderdoc.getelementsbytagname ("money") [number]);
var status =isnull (Orderdoc.getelementsbytagname ("status") [number]);
var docnum =isnull (Orderdoc.getelementsbytagname ("DocNum") [number]);
VAR score =isnull (Orderdoc.getelementsbytagname ("score") [number]);
var std= ' <li><b> URL: ' + (time) + ' <br/> Station name: ' + (place) + ' <br/> Description: ' + (reason) + ' </b></li > ';
return STD;
}
Get page Content
function GetContent () {
Exbrows ();
Orderdoc=loadxmlfile ("Http://www.ok22.org/download/ex2.xml");
var items=orderdoc.getelementsbytagname ("Results"). Length;
Alert (Orderdoc.getelementsbytagname ("Item"). length);
var htmlstr= "";
stringsss+= ' <ul> ';
for (i=0;i<items;i++) {
Stringsss+=getdatabyid (i);
}
stringsss+= ' </ul> '
document.write (STRINGSSS);
}
</script>

<body>
</body>
<script type= "Text/javascript" language= "JavaScript" >
GetContent ();
</script>


Because XML is likely to be empty, it adds a decision to determine if the node is empty!
To determine that a child node is not empty
function IsNull (obj)
{
var nodevalue = "";
if (Obj.childnodes[0]!= null)
{
NodeValue =obj.childnodes[0].nodevalue;
}
return nodevalue;
}



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.