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>