Xml Xpath简单问题
以下代码适用于保存的XML文件,但不会解析HTMLXml Xpath简单问题,xml,xpath,Xml,Xpath,以下代码适用于保存的XML文件,但不会解析HTML <html> <body> <script type="text/javascript"> function loadXMLDoc(dname) { if (window.XMLHttpRequest) { xhttp=new XMLHttpRequest(); } else { xhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xhttp
<html>
<body>
<script type="text/javascript">
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}
xml=loadXMLDoc("test.html");
path="div[@id='aa']/span[@class]//a[@href]"
// code for IE
if (window.ActiveXObject)
{
var nodes=xml.selectNodes(path);
for (i=0;i<nodes.length;i++)
{
document.write(nodes[i].childNodes[0].nodeValue);
document.write("<br />");
}
}
// code for Mozilla, 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>
函数loadXMLDoc(dname)
{
if(window.XMLHttpRequest)
{
xhttp=newXMLHttpRequest();
}
其他的
{
xhttp=新的ActiveXObject(“Microsoft.XMLHTTP”);
}
xhttp.open(“GET”、dname、false);
xhttp.send(“”);
返回xhttp.responseXML;
}
xml=loadXMLDoc(“test.html”);
path=“div[@id='aa']/span[@class]//a[@href]”
//IE代码
if(window.ActiveXObject)
{
var nodes=xml.selectNodes(路径);
对于(i=0;i我对“Microsoft.XMLHTTP”一无所知,但如果您想使用XML解析器,我怀疑您需要将HTML替换为。普通HTML不遵守XML的所有规则(例如,您可以使用未关闭的标记,如
).已添加,但无法解析。现在它是有效的HTML,但解析仍然无效。您提供的代码示例不是有效的HTML(或XHTML)。所有内容都按原样打开和关闭。为什么无效?您已修复了我的上一条注释和您的上一条注释之间的一些错误,但仍然存在错误。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html"; charset="UTF-8" />
</head>
<body>
<div id="aa">
<span class="bb">
<a href="http://google.com">link 1</a>
</span>
</div>
</body>
</html>