admin管理员组文章数量:1419168
How can I get the value of my xml data using a javascript. Im accessing my xml file on my domain, and view it on the client side.
my.xml
<usr>
<uid trk="1234">
<getThis>kdzbnya</getThis>
</uid>
</usr>
I want to get the value of "getThis"
sample.js
function alertThis(){
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var xmlFile = "my.xml";
xmlDoc.async="false";
xmlDoc.load(xmlFile);
xmlObj=xmlDoc.documentElement;
try {
var v = "";
$.each(xmlObj.childNodes, function(i, valThis) {
if(valThis.getAttribute("trk") == "1234"){
v += valThis.getElementsByTagName('getThis').nodeValue;
}
});
alert(v);
}
catch(e){
alert(e);
}
}
but it returns undefined value.
How can I get the value of my xml data using a javascript. Im accessing my xml file on my domain, and view it on the client side.
my.xml
<usr>
<uid trk="1234">
<getThis>kdzbnya</getThis>
</uid>
</usr>
I want to get the value of "getThis"
sample.js
function alertThis(){
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
var xmlFile = "my.xml";
xmlDoc.async="false";
xmlDoc.load(xmlFile);
xmlObj=xmlDoc.documentElement;
try {
var v = "";
$.each(xmlObj.childNodes, function(i, valThis) {
if(valThis.getAttribute("trk") == "1234"){
v += valThis.getElementsByTagName('getThis').nodeValue;
}
});
alert(v);
}
catch(e){
alert(e);
}
}
but it returns undefined value.
Share Improve this question asked Jan 13, 2012 at 0:30 Robin Carlo CatacutanRobin Carlo Catacutan 13.7k12 gold badges54 silver badges86 bronze badges 2- you know this wont work in Firefox, right? – dbrin Commented Jan 13, 2012 at 0:35
- @DmitryB yes, the activexObject you mean, but I'm only working this on IE because it's for the windows 7 gadget. – Robin Carlo Catacutan Commented Jan 13, 2012 at 0:37
3 Answers
Reset to default 2Try adding a .item(0)
or [0]
between getElementsByTagName(...)
and .nodeValue
:
v += valThis.getElementsByTagName('getThis').item(0).nodeValue;
You'll need this as getElementsByTagName
returns a NodeList
(which can resemble an Array
). The list won't have a nodeValue
property itself, but the nodes within it should.
I see you're using jQuery.
change
v += valThis.getElementsByTagName('getThis').nodeValue
to
v += $(valThis).find('getThis').text()
See this example http://www.w3schools./xml/xml_parser.asp of an XML parser. But in reality you probably want to use a framework to load the XML and parse it. There is plenty of them out there, check microjs. for the features that you are looking for.
本文标签: Access XML data via javascriptStack Overflow
版权声明:本文标题:Access XML data via javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745305430a2652615.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论