如何使用javascript从xml中获取所选节点 [英] how to get the selected nodes from xml using javascript
本文介绍了如何使用javascript从xml中获取所选节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
< html >
< body >
< script type = text / javascript >
function getTable(xmlDoc){
//将X Var设置为父节点
var x = xmlDoc.getElementsByTagName( SystemRecord.xml);
//'对于每个'循环
for(i = 0; i < x。长度; i ++)
{
< span class =code-attribute> document.write(< tr > < td > 跨度>);
if(x [i] .getElementsByTagName(cl_system)[0] .childNodes.length == 0){
document.write(x [i] .getElementsByTagName(cl_system )[0] .childNodes [0] .nodeValue);
}
}
getTable(xmlDoc.getElementsByTagName(cl_system)[0]);
}
< / script >
< / body >
< / html >
xml文件SystemRecord.xml
<? xml version = 1.0 standalone = yes ? >
< 系统 >
< 系统 >
< cl_system > ret < / cl_system >
< Rtppath > C:\\ < < span class =code-leadattribute> / Rtppath >
< JDFpath > D:数据< / JDFpath >
< Orginalpath > @ fdsf < / Orginalpath >
< 网址 > http:// localhost:1764 / Insert.aspx < / Url >
< UID > http:// localhost:1764 / Insert.aspx < / UID >
< 密码 > qwee < /密码 >
< localpath > D:\ < / localpath >
< thumbnailurl > http:// localhost:1764 / Insert.aspx < / thumbnailurl >
< Printer_path > http:/ /localhost:1764/Insert.aspx< < span class =code-leadattribute> / Printer_path >
< JMF_Url > http:// localhost:1764 / Jmf.aspx < / JMF_Url >
< FTP_Path > http:// localhost:1764 / Jmf.aspx < / FTP_Path >
< / System >
< 系统 >
< cl_system > dsaf < / cl_system >
< Rtppath > fgdfas123 < / Rtppath >
< JDFpath > agfdg < / JDFpath >
< Orginalpath > fga < / Orginalpath >
< 网址 > fgfa < / Url >
< span class =code-keyword>< UID > ragf < / UID >
< span class =code-keyword>< 密码 > vacx < /密码 >
< span class =code-keyword>< localpath > sfdsv < / localpath >
< thumbnailurl > dfdsv < / thumbnailurl >
< Printer_path > gsagf < / Printer_path >
< JMF_Url > vcvfag < / JMF_Url >
< FTP_Path > fdsdavf &l t; / FTP_Path >
< / System >
< / System >
解决方案
javascript代码:
< !DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // EN http:// www .w3.org / TR / xhtml1 / DTD / xhtml1-transitional.dtd >
< html >
< body >
< script >
function loadXMLDoc(dname){
if(window.XMLHttpRequest){
xhttp = new XMLHttpRequest();
}
else {
xhttp = new ActiveXObject(Microsoft.XMLHTTP);
}
xhttp.open(GET,dname,false);
try {xhttp.responseType =msxml-document} catch(err){} //帮助IE
xhttp.send();
返回xhttp;
}
var x = loadXMLDoc(SystemRecord.xml);
var xml = x.responseXML;
path =/ System / System / Rtppath;
// IE
的代码if(window.ActiveXObject || xhttp.responseType ==msxml-document){
xml.setProperty(SelectionLanguage,XPath );
nodes = xml.selectNodes(path);
for(i = 0; i < nodes.length; i ++) {
document.write(nodes [i] .childNodes [0] .nodeValue);
< span class =code-attribute> document.write(< br > ; 跨度>);
}
}
// Chrome,Firefox,Opera等代码。
else if(document.implementation&& document.implementation.createDocument) {
var nodes = xml.evaluate(path,xml,null,XPathResult.ANY_TYPE,null);
var result = nodes.iterateNext();
while(result){
document.write(result.childNodes [0] .nodeValue);
document.write(< br > 跨度>);
result = nodes.iterateNext();
}
}
< / script >
< / body >
< / html >
xmlfile:
< 系统 >
< 系统 >
< cl_system > ret < / cl_system >
< Rtppath > C:\\ < / Rtppath >
< JDFpath > D:数据< / JDFpath >
< Orginalpath > @fdsf < / Orginalpath >
< 网址 > http:// localhost:1764 / Insert.aspx < / Url >
< UID > http:// localhost:1764 / Insert.aspx < / UID >
< 密码 > qwee < /密码 >
< localpath > D:\ < / localpath >
< thumbnailurl > http:// localhost:1764 / Insert。 aspx < / thumbnailurl >
< Printer_path > http:// localhost:1764 / Insert.aspx < / Printer_path >
< JMF_Url > http:// localhost:1764 / Jmf.aspx < / JMF_Url >
< FTP_Path > http:// loc alhost:1764 / Jmf.aspx < / FTP_Path > ;
< / System >
< 系统 >
< cl_system > dsaf < / cl_system >
< Rtppath > fgdfas123 < / Rtppath >
< JDFpath > agfdg < / JDFpath >
< Orginalpath > fga < / Orginalpath >
< Url > fgfa < / url >
< UID > ragf < / UID >
< 密码 > vacx < /密码 >
< localpath > sfdsv < / localpath >
< thumbnailurl > dfdsv < / thumbnailurl >
< Printer_path > gsagf < / Printer_pat h >
< JMF_Url > vcvfag < / JMF_Url >
< FTP_Path > fdsdavf < / FTP_Path >
< /系统 >
< 系统 >
< cl_system > PRO < / cl_system >
< Rtppath > get456 < / Rtppath >
< JDFpath > dfvdsg < / JDFpath >
< Orginalpath > vfcv c < / Orginalpath >
< Url > nvdfvfda < / Url >
< UID > vfcdfb < / UID >
< 密码 > fdgfdg < /密码 >
< localpath > vcb < / localpath >
< thumbnailurl > fdgfgf < / thumbnailurl >
< Printer_path > fgfd < / Printer_path >
< JMF_Url > vcxv xc < / JMF_Url >
< FTP_Path > vvfga < / FTP_Path >
< / System > 跨度>
<html>
<body>
<script type=text/javascript>
function getTable(xmlDoc){
//Set X Var to parent node
var x=xmlDoc.getElementsByTagName("SystemRecord.xml");
//'For each' loop
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
if (x[i].getElementsByTagName("cl_system")[0].childNodes.length == 0) {
document.write(x[i].getElementsByTagName("cl_system")[0].childNodes[0].nodeValue);
}
}
getTable(xmlDoc.getElementsByTagName("cl_system")[0]);
}
</script>
</body >
</html>
xml file SystemRecord.xml
<?xml version="1.0" standalone="yes"?>
<System>
<System>
<cl_system>ret</cl_system>
<Rtppath>C:\\</Rtppath>
<JDFpath>D:data</JDFpath>
<Orginalpath>@fdsf</Orginalpath>
<Url>http://localhost:1764/Insert.aspx</Url>
<UID>http://localhost:1764/Insert.aspx</UID>
<Password>qwee</Password>
<localpath>D:\</localpath>
<thumbnailurl>http://localhost:1764/Insert.aspx</thumbnailurl>
<Printer_path>http://localhost:1764/Insert.aspx</Printer_path>
<JMF_Url>http://localhost:1764/Jmf.aspx</JMF_Url>
<FTP_Path>http://localhost:1764/Jmf.aspx</FTP_Path>
</System>
<System>
<cl_system>dsaf</cl_system>
<Rtppath>fgdfas123</Rtppath>
<JDFpath>agfdg</JDFpath>
<Orginalpath>fga</Orginalpath>
<Url>fgfa</Url>
<UID>ragf</UID>
<Password>vacx</Password>
<localpath>sfdsv</localpath>
<thumbnailurl>dfdsv</thumbnailurl>
<Printer_path>gsagf</Printer_path>
<JMF_Url>vcvfag</JMF_Url>
<FTP_Path>fdsdavf</FTP_Path>
</System>
</System>
解决方案
javascript code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <body> <script> function loadXMLDoc(dname) { if (window.XMLHttpRequest) { xhttp = new XMLHttpRequest(); } else { xhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xhttp.open("GET", dname, false); try { xhttp.responseType = "msxml-document" } catch (err) { } // Helping IE xhttp.send(""); return xhttp; } var x = loadXMLDoc("SystemRecord.xml"); var xml = x.responseXML; path = "/System/System/Rtppath"; // code for IE if (window.ActiveXObject || xhttp.responseType == "msxml-document") { xml.setProperty("SelectionLanguage", "XPath"); nodes = xml.selectNodes(path); for (i = 0; i < nodes.length; i++) { document.write(nodes[i].childNodes[0].nodeValue); document.write("<br>"); } } // code for Chrome, Firefox, Opera, etc. else if (document.implementation && document.implementation.createDocument) { var nodes = xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null); var result = nodes.iterateNext(); while (result) { document.write(result.childNodes[0].nodeValue); document.write("<br>"); result = nodes.iterateNext(); } } </script> </body> </html>
xmlfile:
<System> <System> <cl_system>ret</cl_system> <Rtppath>C:\\</Rtppath> <JDFpath>D:data</JDFpath> <Orginalpath>@fdsf</Orginalpath> <Url>http://localhost:1764/Insert.aspx</Url> <UID>http://localhost:1764/Insert.aspx</UID> <Password>qwee</Password> <localpath>D:\</localpath> <thumbnailurl>http://localhost:1764/Insert.aspx</thumbnailurl> <Printer_path>http://localhost:1764/Insert.aspx</Printer_path> <JMF_Url>http://localhost:1764/Jmf.aspx</JMF_Url> <FTP_Path>http://localhost:1764/Jmf.aspx</FTP_Path> </System> <System> <cl_system>dsaf</cl_system> <Rtppath>fgdfas123</Rtppath> <JDFpath>agfdg</JDFpath> <Orginalpath>fga</Orginalpath> <Url>fgfa</Url> <UID>ragf</UID> <Password>vacx</Password> <localpath>sfdsv</localpath> <thumbnailurl>dfdsv</thumbnailurl> <Printer_path>gsagf</Printer_path> <JMF_Url>vcvfag</JMF_Url> <FTP_Path>fdsdavf</FTP_Path> </System> <System> <cl_system>PRO</cl_system> <Rtppath>get456</Rtppath> <JDFpath>dfvdsg</JDFpath> <Orginalpath>vfcv c</Orginalpath> <Url>nvdfvfda</Url> <UID>vfcdfb</UID> <Password>fdgfdg</Password> <localpath> vcb</localpath> <thumbnailurl>fdgfgf</thumbnailurl> <Printer_path>fgfd</Printer_path> <JMF_Url>vcxv xc</JMF_Url> <FTP_Path>vvfga</FTP_Path> </System>
这篇关于如何使用javascript从xml中获取所选节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文