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>