为什么我无法在 javascript 中解析 xml?
你好,我在解析 xml 时遇到问题.. 我有这样的 xml:
<tejemahan>
<kategori> komputer </kategori>
<hasil> aplikasi komputer </hasil>
</terjemahan>
编辑: 上面的 xml 我就是这样得到的:
var url="http://localhost:8080/inlinetrans/api/translate/"+userSelection+"/"+hasilStemSel+"/"+hasilStem;
var client = new XMLHttpRequest();
client.open("GET", url, false);
client.setRequestHeader("Content-Type", "text/plain");
client.send(null);
if(client.status == 200)
alert("the request success"+client.responseText);
else
alert("the request isn't success"+client.status+""+client.statusText)
}
这是我解析上面 xml 文件的代码:
this.loadXML = function (){
var url = http://localhost:8080/coba/api/artikan/"+sel+"/"+hasilStemSel+"/"+hasilStem
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("url");
xmlDoc.onload= this.readXML;
}
this.readXML = function() {
alert(xmlDoc.documentElement.tagName);
alert(xmlDoc.documentElement.childNodes[0].tagName);
alert(xmlDoc.documentElement.childNodes[1].tagName);
alert(xmlDoc.documentElement.childNodes[0].textContent);
alert(xmlDoc.documentElement.childNodes[1].textContent);
}
我可以执行这段代码
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("url");
,但为什么我不能执行这段代码 xmlDoc.load = this.readXML ???
hello i have problem to parse xml..
i have xml like this :
<tejemahan>
<kategori> komputer </kategori>
<hasil> aplikasi komputer </hasil>
</terjemahan>
Edited:
xml above I get in that way :
var url="http://localhost:8080/inlinetrans/api/translate/"+userSelection+"/"+hasilStemSel+"/"+hasilStem;
var client = new XMLHttpRequest();
client.open("GET", url, false);
client.setRequestHeader("Content-Type", "text/plain");
client.send(null);
if(client.status == 200)
alert("the request success"+client.responseText);
else
alert("the request isn't success"+client.status+""+client.statusText)
}
and this is my code to parse an xml file above :
this.loadXML = function (){
var url = http://localhost:8080/coba/api/artikan/"+sel+"/"+hasilStemSel+"/"+hasilStem
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("url");
xmlDoc.onload= this.readXML;
}
this.readXML = function() {
alert(xmlDoc.documentElement.tagName);
alert(xmlDoc.documentElement.childNodes[0].tagName);
alert(xmlDoc.documentElement.childNodes[1].tagName);
alert(xmlDoc.documentElement.childNodes[0].textContent);
alert(xmlDoc.documentElement.childNodes[1].textContent);
}
i can execute this code
xmlDoc=document.implementation.createDocument("","",null);
xmlDoc.load("url");
but why i can't execute this code
xmlDoc.load = this.readXML ???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将 onload 处理程序分配放在 load() 调用之前。如果您先调用 load(),onload 事件将在您分配处理程序来处理它之前发生。像这样:
Try putting the onload handler assignment before the load() call. If you call load() first, the onload event will happen before you have assigned a handler to handle it. Like this:
首先,我赞同 David Dorward 的建议:使用
XMLHttpRequest
代替,它适用于所有主流浏览器。代码如下。其次,您的 readXML 函数存在缺陷,因为大多数浏览器都会在 childNodes 集合中包含空白文本节点,因此 xmlDoc.documentElement.childNodes[0] 实际上是一个文本节点,并且没有
tagName
属性。我建议在迭代childNodes
时使用getElementsByTagName()
或检查每个节点的nodeType
属性。第三,您的 XML 无效:
和不匹配,尽管这可能是您问题中的拼写错误。
Firstly, I second David Dorward's suggestion: use
XMLHttpRequest
instead, which will work in all major browsers. Code is below.Secondly, your
readXML
function is flawed, since most browsers will include whitespace text nodes within thechildNodes
collection, soxmlDoc.documentElement.childNodes[0]
will actually be a text node and have notagName
property. I would suggest usinggetElementsByTagName()
or checking thenodeType
property of each node as you iterate overchildNodes
.Thirdly, your XML is not valid: the
<tejemahan>
and</terjemahan>
do not match, although this may be a typo in your question.