admin管理员组文章数量:1290939
Given an XML element in jQuery like so:
$('<foo oy="vey" foo="bar" here="is" another="attribute" />')
Can I use either jQuery or plain old JavaScript to get an array containing the names of all the attributes in an XML element? I would expect this:
['oy','foo','here','another']
Given an XML element in jQuery like so:
$('<foo oy="vey" foo="bar" here="is" another="attribute" />')
Can I use either jQuery or plain old JavaScript to get an array containing the names of all the attributes in an XML element? I would expect this:
['oy','foo','here','another']
Share
Improve this question
edited Feb 10, 2015 at 22:36
Deduplicator
45.7k7 gold badges72 silver badges123 bronze badges
asked Nov 10, 2009 at 3:42
Daniel SchafferDaniel Schaffer
57.9k32 gold badges118 silver badges166 bronze badges
2
- 2 A word of caution, jquery does not really support forming XML from a string literal. It will work in firefox and maybe in other browsers, but not IE. See docs.jquery./Core/jQuery : "A string of HTML to create on the fly. Note that this parses HTML, not XML." – Funka Commented Nov 10, 2009 at 4:15
- Oh, I should also mention, there are plugins that will allow you to do this if you want. – Funka Commented Nov 10, 2009 at 4:16
3 Answers
Reset to default 6The jQuery function isn't really meant to parse XML, it can parse HTML, but it's not really the same.
What about using the browser's XML parser:
function parseXML(text) {
var parser, xmlDoc;
if (window.DOMParser) {
parser = new DOMParser();
xmlDoc = parser.parseFromString(text,"text/xml");
} else { // IE
xmlDoc= new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(text);
}
return xmlDoc;
}
// Demo
var doc = parseXML('<foo oy="vey" foo="bar" here="is" another="attribute" />');
var foo = doc.childNodes[0];
for (var i = 0; i < foo.attributes.length; i++) {
var attr = foo.attributes[i];
alert(attr.name + " = " + attr.value);
}
Run the above code here.
This plugin will help you do that.
You can also do it using plain old javascript using something like that :
var elt = document.getElementsByTagName('id');
for (i=0;i<elt.attributes.length;i++){
//elt.attributes[i].nodeName is what you want, .nodeValue for its value.
}
A) Single <Foo>
element
Do you need list of attributes of a single element?
... if so - do you really need an array?
Simple $('<foo ... />').get(0).attributes
...will give you NamedNodeMap (object) of attributes
B) All elements <Foo>
in the whole (XML) document
@Soufiane Hassou 's answer is showing approach but is missing the inner loop...
Do you need to fetch all possible attribute names of an element (e.g. Product element) inside a whole XML document?
var yourElements = document.getElementsByTagName('Foo'); //get all <Foo> elements
var listOfAttributeNames = []; //prepare empty array for attribute names
var attributeNameBuffer; //buffer for current attribute name in loop
//Loop all elements
for(var i = 0; i < yourElements.length ; ++i){
//Loop all attributes of a current element
for( k = 0 ; k < yourElements[i].attributes.length ; ++k ){
//Temporary store current attribute name
attributeNameBuffer = yourElements[i].attributes[k].name;
//First,
//test if the attribute name does not already exist in our array of names
if( listOfAttributeNames.indexOf(attributeNameBuffer) == -1 )
listOfAttributeNames.push( attributeNameBuffer ); //if not, add it
}
}
console.log(listOfAttributeNames); //display array of attributes in console
本文标签: JavaScriptjQuery How do I get an array of all attributes in an XML elementStack Overflow
版权声明:本文标题:JavaScriptjQuery: How do I get an array of all attributes in an XML element? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741505100a2382273.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论