使用 javascript 获取单个 xml 节点
这应该非常简单,但由于某种原因我似乎无法让它工作; 所以我有一个 XML 文件,如下所示
<board>
<version>1</version>
<r>
<c>
<tile>g</tile>
</c>
<c>
<tile>B</tile>
</c>
</r>
<r>
<c>
<tile>C</tile>
</c>
<c>
<tile>D</tile>
</c>
</r>
</board>
: 和一些 JavaScript,例如:
function get_cversion(){
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "board.XML", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
var mytext = 0
var x=xmlDoc.getElementsByTagName("version");
mytext = (x[0].childNodes[0].nodeValue);
mytext += "";
document.frmOne.input1.value = ""+mytext;
}
最后,我在页面上有一个如下所示的表单:
<FORM NAME = frmOne>
1val: <INPUT TYPE = Text NAME = input1 SIZE = 4 value ="">
<p>
<Input Type = Button NAME = b1 VALUE = "Save val" onClick = update_XX()>
<p>
<Input Type = Button NAME = b2 VALUE = "WOOOOO" onClick = get_cversion()>
</FORM>
我对整个 XML 游戏还是很陌生。我知道我一定错过了一些非常明显的东西,但我只是看不到它,任何帮助将不胜感激。
谢谢。
This should be really simple but for some reason I can't seem to get it to work;
So I have a XML file as follows:
<board>
<version>1</version>
<r>
<c>
<tile>g</tile>
</c>
<c>
<tile>B</tile>
</c>
</r>
<r>
<c>
<tile>C</tile>
</c>
<c>
<tile>D</tile>
</c>
</r>
</board>
And some JavaScript like:
function get_cversion(){
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "board.XML", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
var mytext = 0
var x=xmlDoc.getElementsByTagName("version");
mytext = (x[0].childNodes[0].nodeValue);
mytext += "";
document.frmOne.input1.value = ""+mytext;
}
and last, I have a form on the page like this:
<FORM NAME = frmOne>
1val: <INPUT TYPE = Text NAME = input1 SIZE = 4 value ="">
<p>
<Input Type = Button NAME = b1 VALUE = "Save val" onClick = update_XX()>
<p>
<Input Type = Button NAME = b2 VALUE = "WOOOOO" onClick = get_cversion()>
</FORM>
I'm still really new at this whole XML game. I know I must be missing something really obvious but I just cant see it, any help would be greatly appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要在处理 XML 之前检查请求的状态。你的代码应该是这样的,
You might want to check the status of the request before processing the XML. Your code should look something like this,