使用JavaScript访问XML中的lastNode值在Chrome中不起作用
我是使用XML进行JavaScript编程的新手。我尝试了《XML内部》一书中的以下示例,但无法使其运行 以下是使用JavaScript的HTML代码:使用JavaScript访问XML中的lastNode值在Chrome中不起作用,javascript,xml,google-chrome,Javascript,Xml,Google Chrome,我是使用XML进行JavaScript编程的新手。我尝试了《XML内部》一书中的以下示例,但无法使其运行 以下是使用JavaScript的HTML代码: <HTML> <HEAD> <TITLE> Reading XML element values </TITLE> <SCRIPT LANGUAGE="JavaScript">
<HTML>
<HEAD>
<TITLE>
Reading XML element values
</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function readXMLDocument()
{
var xmldoc, meetingsNode, meetingNode, peopleNode
var first_nameNode, last_nameNode, outputText
<!--xmldoc = new ActiveXObject("Microsoft.XMLDOM")-->
<!--xmldoc.load("meetings.xml")-->
parser=new DOMParser();
xmldoc=parser.parseFromString("meetings.xml","text/xml");
meetingsNode = xmldoc.documentElement
meetingNode = meetingsNode.firstChild
peopleNode = meetingNode.lastChild
personNode = xmldoc.getElementsByTagName("PEOPLE").lastChild
first_nameNode = personNode.firstChild
last_nameNode = first_nameNode.nextSibling
outputText = "Third name: " +
first_nameNode.lastChild.nodeValue + ' '
+ last_nameNode.lastChild.nodeValue
messageDIV.innerHTML=outputText
}
</SCRIPT>
</HEAD>
<BODY>
<CENTER>
<H1>
Reading XML element values
</H1>
<INPUT TYPE="BUTTON" VALUE="Get the name of the third person"
ONCLICK="readXMLDocument()">
<P>
<DIV ID="messageDIV"></DIV>
</CENTER>
</BODY>
</HTML>
请帮我解决这个问题。提前谢谢。我使用Firefox和firebug插件进行调试,但您可以使用Chrome而不使用任何插件。加载页面后,可以按F12打开devtools。在控制台选项卡中,您可以看到日志、警告、错误的输出,并且有一个执行JavaScript的命令行 有时我会将一个变量设为全局变量(在代码中签入变量a),这样我就可以在命令行中键入
a.
,查看它有哪些属性
问题之一是节点包含空格,因此在许多情况下,最后一个节点是包含空格的textNode。你也应该试着用一句话来结束你的陈述
下面是经过半调试的代码,希望对您有所帮助:
<textarea id="txt">your xml content</textarea>
function readXMLDocument(){
var xmldoc, meetingsNode, meetingNode, peopleNode,
first_nameNode, last_nameNode, outputText;
<!--xmldoc = new ActiveXObject("Microsoft.XMLDOM")-->
<!--xmldoc.load("meetings.xml")-->
parser=new DOMParser();
xmldoc=parser.parseFromString(
document.getElementById("txt").value,"text/xml");
meetingsNode = xmldoc.documentElement;
meetingNode = meetingsNode.firstChild;
console.log("meetingNode is:",meetingNode);
peopleNode = meetingNode.lastChild;
console.log("peoplenode is:",peopleNode);
//use console.log to figure out what the variable could be
console.log(xmldoc.getElementsByTagName("PEOPLE"));
//set a global variable named a to the what you want to inspect
//in the commandline you can type a. and after the dot the devtools
//will produce a list of attributes.
a = xmldoc.getElementsByTagName("PEOPLE");
personNode = xmldoc.getElementsByTagName("PEOPLE")
.item(0).lastElementChild;
a = personNode;
//in the commandline I can see a.lastChild is textnode
//the intellisense gives me an option lastElementChild
//didn't look it up but it could be Firefox specific
first_nameNode = personNode.lastElementChild;
last_nameNode = first_nameNode.nextSibling;
console.log("there are still some things to fix:",
meetingsNode, meetingNode, peopleNode,
first_nameNode, last_nameNode);
return;
outputText = "Third name: " +
first_nameNode.lastChild.nodeValue + ' '
+ last_nameNode.lastChild.nodeValue;
}
readXMLDocument();
您的xml内容
函数readXMLDocument(){
变量xmldoc、meetingsNode、meetingNode、PeopNode、,
第一个名称节点、最后一个名称节点、输出文本;
parser=新的DOMParser();
xmldoc=parser.parseFromString(
document.getElementById(“txt”).value,“text/xml”);
meetingsNode=xmldoc.documentElement;
meetingNode=meetingsNode.firstChild;
log(“meetingNode为:”,meetingNode);
peopleNode=meetingNode.lastChild;
log(“peoplenode为:”,peoplenode);
//使用console.log确定变量可能是什么
log(xmldoc.getElementsByTagName(“人”);
//将名为a的全局变量设置为要检查的对象
//在命令行中,您可以键入a.并在点后面键入devtools
//将生成属性列表。
a=xmldoc.getElementsByTagName(“人”);
personNode=xmldoc.getElementsByTagName(“人”)
.项目(0).lastElementChild;
a=个人节点;
//在命令行中,我可以看到a.lastChild是textnode
//智能感知给了我一个选项lastElementChild
//没有查找,但可能是特定于Firefox的
first_nameNode=personNode.lastElementChild;
last_nameNode=first_nameNode.nextSibling;
log(“仍有一些问题需要解决:”,
meetingsNode、meetingNode、PeopNode、,
第一个名称节点,最后一个名称节点);
返回;
outputText=“第三个名称:”+
第一个名称node.lastChild.nodeValue+“”
+last_nameNode.lastChild.nodeValue;
}
readXMLDocument();
Uncaught TypeError: Cannot read property 'nodeValue' of null
<textarea id="txt">your xml content</textarea>
function readXMLDocument(){
var xmldoc, meetingsNode, meetingNode, peopleNode,
first_nameNode, last_nameNode, outputText;
<!--xmldoc = new ActiveXObject("Microsoft.XMLDOM")-->
<!--xmldoc.load("meetings.xml")-->
parser=new DOMParser();
xmldoc=parser.parseFromString(
document.getElementById("txt").value,"text/xml");
meetingsNode = xmldoc.documentElement;
meetingNode = meetingsNode.firstChild;
console.log("meetingNode is:",meetingNode);
peopleNode = meetingNode.lastChild;
console.log("peoplenode is:",peopleNode);
//use console.log to figure out what the variable could be
console.log(xmldoc.getElementsByTagName("PEOPLE"));
//set a global variable named a to the what you want to inspect
//in the commandline you can type a. and after the dot the devtools
//will produce a list of attributes.
a = xmldoc.getElementsByTagName("PEOPLE");
personNode = xmldoc.getElementsByTagName("PEOPLE")
.item(0).lastElementChild;
a = personNode;
//in the commandline I can see a.lastChild is textnode
//the intellisense gives me an option lastElementChild
//didn't look it up but it could be Firefox specific
first_nameNode = personNode.lastElementChild;
last_nameNode = first_nameNode.nextSibling;
console.log("there are still some things to fix:",
meetingsNode, meetingNode, peopleNode,
first_nameNode, last_nameNode);
return;
outputText = "Third name: " +
first_nameNode.lastChild.nodeValue + ' '
+ last_nameNode.lastChild.nodeValue;
}
readXMLDocument();