XML Functions in Javascript: XML Parser, XML Serializer, XPATH
My = {};
My.xml = {
    node : {
        ELEMENT_NODE : 1, // Element
        ATTRIBUTE_NODE : 2, // Attr
        TEXT_NODE : 3, // Text
        CDATA_SECTION_NODE : 4, // CDATASection
        PROCESSING_INSTRUCTION_NODE : 7, // ProcessingInstruction
        COMMENT_NODE : 8, // Comment
        DOCUMENT_NODE : 9, // Document
        DOCUMENT_TYPE_NODE : 10, // DocumentType
        DOCUMENT_FRAGMENT_NODE : 11
        // DocumentFragment
    },
    parse : function(txt) {
        var doc;
        if (typeof ActiveXObject != 'undefined') {
            // Internet Explorer
            doc = new ActiveXObject("Microsoft.XMLDOM");
            doc.async = "false";
            doc.loadXML(txt);
        } else if (typeof DOMParser != 'undefined') {
            // Mozilla Firefox
            doc = (new DOMParser()).parseFromString(txt, 'text/xml');
        } else {
            var url = 'data:text/xml;charset=utf-8,' + encodeURIComponent(txt);
            var request = new XMLHttpRequest();
            request.open('GET', url, false);
            request.send(null);
            doc = request.responseXML;
        }
        return doc;
    },
    serialize : function(xmlObj) {
        if (typeof xmlObj == 'object') {
            var doc = xmlObj._xml ? xmlObj._xml : xmlObj;
            if (doc.xml) {
                // Internet Explorer
                return doc.xml;
            } else if (typeof XMLSerializer !== 'undefined') {
                return (new XMLSerializer()).serializeToString(doc);
            }
        }
        throw new Error('Cannot convert/serialize XML node: ' + xmlObj + '.');
    },
    xnodes : function(xe, xpath) {
        var nodes;
        if (window.ActiveXObject) {
            nodes = xe.selectNodes(xpath);
        } else if (window.XPathEvaluator) {
            var result = (new XPathEvaluator()).evaluate(xpath, xe, null,
                    XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
            nodes = [];
            if (typeof result != 'undefined' && result != null) {
                var node = result.iterateNext();
                while (node) {
                    nodes.push(node);
                    node = result.iterateNext();
                }
            }
        } else {
            throw new Error('Failed to perform xpath. Your browser does not support XPATH.');
        }
        return nodes;
    },
    xnode : function(xe, xpath) {
        var node;
        if (window.ActiveXObject) {
            node = xe.selectSingleNode(xpath);
        } else if (window.XPathEvaluator) {
            // FIRST_ORDERED_NODE_TYPE returns the first match to the xpath.
            var result = (new XPathEvaluator()).evaluate(xpath, xe, null,
                    XPathResult.FIRST_ORDERED_NODE_TYPE, null);
            if (typeof result != 'undefined' && result != null) {
                node = result.singleNodeValue;
            }
        } else {
            throw new Error('Failed to perform xpath. Your browser does not support XPATH.');
        }
        return node;
    },
    xvalues : function(xe, xpath) {
        var values = [];
        var nodes = this.xnodes(xe, xpath);
        if (typeof nodes !== 'undefined' && nodes != null) {
            for (var i = 0; i < nodes.length; i++) {
                var node = nodes[i];
                switch (node.nodeType) {
                    case this.node.ATTRIBUTE_NODE :
                        values.push(node.nodeValue);
                        break;
                    case this.node.ELEMENT_NODE :
                        node.normalize();
                        var text;
                        for (var j = 0; j < node.childNodes.length; j++) {
                            var childNode = node.childNodes[j];
                            if (childNode.nodeType === this.node.TEXT_NODE) {
                                text = childNode.nodeValue;
                                break;
                            }
                        }
                        if (typeof text !== 'undefined' && text != null) {
                            values.push(text);
                        }
                        break;
                }
            }
        }
        return values;
    },
    xvalue : function(xe, xpath) {
        var value
        var node = this.xnode(xe, xpath);
        if (node) {
            if (node.nodeType === this.node.ATTRIBUTE_NODE) {
                value = node.nodeValue;
            } else if (node.nodeType === this.node.ELEMENT_NODE) {
                node.normalize();
                for (var j = 0; j < node.childNodes.length; j++) {
                    var childNode = node.childNodes[j];
                    if (childNode.nodeType === this.node.TEXT_NODE) {
                        value = childNode.nodeValue;
                        break;
                    }
                }
            }
        }
        return value;
    }
};
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License