views:

83

answers:

2

Hi. 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]

A: 

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")

David Dorward
a fragment of a document received from SOAP/ajax.
Alexey
a single element with namespaces actually.
Alexey
A: 

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;
            }
        }
    }
Alexey
I should elaborate the xpath expression as it only selects the first attribute in the document
Alexey