0

Internet Explorer has support for xml response parsing and lets you do

element.xml.attributes.getNamedItem('myAttr')

How can I do the same for an xml element in standards based browsers?

The type of element is [object Element]

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Alexey
  • 414
  • 3
  • 10

2 Answers2

0

Use the standard DOM instead of Microsoft's propriety approach. (Assuming you are talking about a proper XML document and not an IE4 era "XML data island")

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

OK, derived the answer using FireBug from here: Versatile xml attribute regex with javascript

element.ownerDocument.evaluate("//@"+attributeName, element.ownerDocument, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null).iterateNext().nodeValue;

        function GetAttributeValueFromXmlElement(element, attrName) {
        if (element && element.xml) {
            //msie
            return $(element.xml)[0].attributes.getNamedItem(attrName).value;
        } else {
            //standard DOM
            if (true/*Should check if there's only one here and only where expected to be*/) {
                return element.ownerDocument
                    .evaluate("//@" + attrName, element.ownerDocument, null
                    , XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null)
                    .iterateNext().nodeValue;
            }
        }
    }
Community
  • 1
  • 1
Alexey
  • 414
  • 3
  • 10
  • I should elaborate the xpath expression as it only selects the first attribute in the document – Alexey Jul 22 '10 at 06:02