How to read xml objects parsed by javascript

Source: Internet
Author: User

Read xml:
Copy codeThe Code is as follows:
<Svg id = "svgID" key = "1">
<Desc id = "descID">
Text1
</Desc>
<Defs>
Text2
</Defs>
<G>
Text3
</G>
</Svg>

The xml Parsing Method of javascript is as follows:
Copy codeThe Code is as follows:
<Html xmlns = "http://www.w3.org/1999/xhtml" xml: lang = "en" lang = "en">
<Head>
<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.doc umentElement; // get the svg object
XmlDoc. text; // output all text, output: text1 Text2 Text3 In the svg object
XmlDoc. getAttribute ("id"); // output the id of the svg object, output: svgID
XmlDocxmlDocLen = xmlDoc. childNodes. length; // Number of subnodes of the output svg object, output: 3
Var xmlDocxmlDocChilds = xmlDoc. childNodes; // obtain all sub-nodes of svg.
Var xmlDOcChildNode0 = xmlDocChilds [0]; // obtain the first subnode of svg
Var xmlDocChildNode0TagName = xmlDOcChildNode0.nodeName; // get the tagname of the first sub-node of svg, output: desc
Var xmlDocChildNode0ID = xmlDOcChildNode0.getAttribute ("id"); // obtain the id attribute of the first sub-node of svg, output: descID
Var xmlDocChildNode0InnerText = xmlDOcChildNode0.Text
XmlDocChildNode0InnerText = xmlDOcChildNode0.firstChild. nodeValue; // obtain the text of the first child node of svg, output: Text1
}
// --> </Script>
<Title> </title>
</Head>
<Body>
<Input onclick = "initXML ();" value = "testXML" type = "button"/>
</Body>
</Html>

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.