Script _javascript technique for JavaScript to get the number of child nodes of a node in XML

Source: Internet
Author: User
A.xml:
Copy Code code as follows:

<?xml version= "1.0" encoding= "gb2312"?>
<pnode>
<node xmlid= "0"/>
<node xmlid= "1"/>
<node xmlid= "2"/>
<node xmlid= "3"/>
<node xmlid= "4"/>
</pnode>

javascript:
Copy Code code as follows:

<script language= "javascript" type= "Text/javascript" >
Load XML document
function Loadxml (xmlFile)
{
var xmldoc;
if (window. ActiveXObject)
{
xmldoc = new ActiveXObject (' Microsoft.XMLDOM ');
Xmldoc.async = false;
Xmldoc.load (XmlFile);
}
else if (document.implementation&&document.implementation.createdocument)
{
xmldoc = Document.implementation.createDocument (",", null);
Xmldoc.load (XmlFile);
}
Else
return null;
return xmldoc;
}
First, the XML object is judged
function Checkxmldocobj (xmlFile)
{
var xmldoc = Loadxml (xmlFile);
if (xmldoc==null)
{
Alert (' Your browser does not support XML file reads, so this page prohibits your operation, the recommended use of IE5.0 above can solve this problem! ');
return false;
}
return xmldoc;
}
var xmldoc=checkxmldocobj ("A.xml");
if (window. ActiveXObject) Xmldoc.removechild (Xmldoc.childnodes[0]);
var topm=xmldoc.getelementsbytagname ("Node1") [0].childnodes;
alert (topm.length);
</script>
Related Article

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.