The reading method of JavaScript parsing XML object _javascript technique of fine solution

Source: Internet
Author: User
Read xml:
Copy Code code as follows:

<svg id= "Svgid" key= "1" >
<desc id= "Descid" >
Text1
</desc>
<defs>
Text2
</defs>
<g>
Text3
</g>
</svg>

JavaScript parses this XML in the following ways:
Copy Code code as follows:

<script type= "Text/javascript" >
<!--
function Initxml () {
if (window. ActiveXObject) {
var doc=new activexobject ("Microsoft.XMLDOM");
Doc.async= "false";
Doc.load ("Test.xml");
}else{
var parser=new domparser ();
var doc=parser.parsefromstring ("Test.xml", "Text/xml");
}
var xmldoc=doc.documentelement;//get SVG Object
xmldoc.text;//output All Text,output:text1 Text2 in an SVG object Text3
Xmldoc.getattribute ("id");//Output Id,output:svgid of SVG objects
xmldocxmldoclen=xmldoc.childnodes.length;//output the number of child nodes of the SVG object, Output:3
var xmldocxmldocchilds=xmldoc.childnodes;//gets all the child nodes of SVG
var xmldocchildnode0=xmldocchilds[0];//gets the first child node of SVG
var xmldocchildnode0tagname=xmldocchildnode0.nodename;//gets the tagname,output:desc of the first child node of SVG
var xmldocchildnode0id=xmldocchildnode0.getattribute ("id");//Get the id attribute of the first child node of SVG, Output:descid
var xmldocchildnode0innertext=xmldocchildnode0.text
xmldocchildnode0innertext=xmldocchildnode0.firstchild.nodevalue;;/ /Gets the text,output:text1 of the first child node of SVG
}
--></script>
<title></title>
<body>
<input onclick= "Initxml ();" Value= "Testxml" type= "button"/>
</body>

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.