开发者

I want the original case-sensitve tagName in XML element.

The following code returns UpperCase text of the tagName. I want the original case.

var xml = '<myElements type="AA" coID="A923"><myHouse>01</myHouse> <myCars>02</myCars><myWifeAndDog>03</myWifeAndDog></myElements>';

$(xml).children().each(function () {
    var xmlnode = $(this);
    console.log(this.tagName + " - " + xmlnode.text());
});

Returns:

MYHOUSE - 01
MYCARS - 02
MYWIFEANDDOG - 03

I want it to return:

myHouse - 01
myCars- 02
myWifeAndDog- 03

How can i do this?

SOLVED: Using DOM as Mike suggested. Not sure if this is supported by ALL browsers though.

var xmlDoc;
if (window.DOMParser) {
    parser = new DOMParser();
    xmlDoc = parser.parseFromString(data.xmltext, "text/xml");
}
else // Internet Explorer
{
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.as开发者_StackOverflow社区ync = "false";
    xmlDoc.loadXML(data.xmltext);
}
console.log(xmlDoc);
console.log(xmlDoc.firstChild.children[0].tagName);


Try using $.parseXML before passing the xml to the $() function.

var $xml = $.parseXML(xml);

$('myElements > *', $xml).each(function(){
    var xmlnode = $(this);
    console.log(this.tagName+" - "+xmlnode.text());
});

Demo: http://jsfiddle.net/97a7T/


May be you shouldn't use jQuery for that and use the DOM like:

var parser = new DOMParser(),
    xmlStr = '<myElements type="A" coID="A92"><myHouse>1</myHouse></myElements>',
    dom = parser.parseFromString(xmlStr, "text/xml");
    console.log(dom.firstChild.tagName);

This is ok for Firefox, Chrome. IE has another way to read and load XML, but similar in the end.


when you use .tagName on an HTML tag it will always return you the tagname in upper case where as when you use .tagName on the XML node the case is preserved

here is the reference http://reference.sitepoint.com/javascript/Element/tagName

they say that in opera the case is preserved on HTML tags, you can try it ...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜