xml文档:books.xml<bookstore>
<book category="children">
<title lang="en">sky</title>
<author></author>
</book>
</bookstore>js文档:
<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"></script>
</head>
<body><script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
txt=x.nodeValue;
document.write("title="+txt);y=xmlDoc.getElementsByTagName("author")[0].childNodes[0];
txt1=y.nodeValue;
document.write("author="+txt1);
</script>
</body>
</html>遍历节点的时候可以得到 title=sky
为何得不到author=
是不是因为author节点的值为空,我想得到author= 如何编写程序呢?
附:
loadxmldoc.js文档:function loadXMLDoc(dname)
{
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
catch(e)
{
try //Firefox, Mozilla, Opera, etc.
{
xmlDoc=document.implementation.createDocument("","",null);
}
catch(e) {alert(e.message)}
}
try
{
xmlDoc.async=false;
xmlDoc.load(dname);
return(xmlDoc);
}
catch(e) {alert(e.message)}
return(null);
}
<book category="children">
<title lang="en">sky</title>
<author></author>
</book>
</bookstore>js文档:
<html>
<head>
<script type="text/javascript" src="/example/xdom/loadxmldoc.js"></script>
</head>
<body><script type="text/javascript">
xmlDoc=loadXMLDoc("/example/xdom/books.xml");x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
txt=x.nodeValue;
document.write("title="+txt);y=xmlDoc.getElementsByTagName("author")[0].childNodes[0];
txt1=y.nodeValue;
document.write("author="+txt1);
</script>
</body>
</html>遍历节点的时候可以得到 title=sky
为何得不到author=
是不是因为author节点的值为空,我想得到author= 如何编写程序呢?
附:
loadxmldoc.js文档:function loadXMLDoc(dname)
{
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
catch(e)
{
try //Firefox, Mozilla, Opera, etc.
{
xmlDoc=document.implementation.createDocument("","",null);
}
catch(e) {alert(e.message)}
}
try
{
xmlDoc.async=false;
xmlDoc.load(dname);
return(xmlDoc);
}
catch(e) {alert(e.message)}
return(null);
}
<book category="children">
<title lang="en">sky</title>
<author/>
</book>
</bookstore>
判断一下
txt1= y==null ? "" : y.nodeValue;