代码之家  ›  专栏  ›  技术社区  ›  Raul Agrait

忽略JavaScript中的空XML节点

  •  2
  • Raul Agrait  · 技术社区  · 16 年前

    我是 loading in an XML file childNodes ,它们是文档中的所有XML元素。我遇到的问题是,我需要忽略不是真实元素的元素,而是换行符、制表符等。现在我正在执行以下操作:

    for (var i = 0; i < childList.length; i++)
    {
        switch (childList[i].nodeType)
        {
            case 1: // Node.ELEMENT_NODE
                /* snip */
                break;
            case 3: // Node.TEXT_NODE
            case 8: // Node.COMMENT_NODE
    
                // Ensure the node is a valid node, and not newlines, tabs, etc
                if (!nodeEmpty(childList[i].nodeValue)
                {
                    // do stuff
                }
    
                break;
        }
    }
    
    function nodeEmpty(nodeValue)
    {
        var isEmpty = true;
        var length = nodeValue.length;
        for (var i = 0; i < length; i++)
        {
            if (nodeValue[i] != ' ' && nodeValue[i] != '\n' && nodeValue[i] != '\t')
            {
                isEmpty = false;
                break;
            }
        }
    
        return isEmpty;
    }
    

    但这似乎是一种非常不优雅的实现方式。有更好的方法吗?

    1 回复  |  直到 9 年前
        1
  •  3
  •   Breton    16 年前
    /**
     * xmlsimplify simplifies an XML object by copying it into a
     * javascript object with only a subset of the attributes of the original.
     * This makes it suitable for output as JSON, or for further processing by
     * other functions.
    
     * @param {Object} xml
     * @param {Boolean} strip if true, strip empty (whitespace) nodes
     */
    var xmlsimplify = function(xml, strip) {
        var obj = {};
        if (typeof xml === "string") {
            xml = getXmlDocument(xml);
        }
        var traverse = function(node) {
            var i, l, n, a, j;
            if (node.nodeType) {
                var o = {};
                switch (node.nodeType) {
                case 1:
                    //element node;
                    o = {
                        nodeName: node.nodeName
                    }; //record nodename
                    for (i = 0, l = node.attributes.length, n = node.attributes; i < l; i++) { //append attributes
                        a = traverse(n.item(i));
                        for (j in a) {
                            if (a.hasOwnProperty(j)) {
                                o[j] = a[j];
                            }
                        }
                    }
                    if (node.childNodes.length) {
                        o.childNodes = [];
                        for (i = 0, l = node.childNodes.length, n = node.childNodes; i < l; i++) {
                            a = traverse(n.item(i));
                            if (a !== null) {
                                o.childNodes.push(a);
                            }
                        }
                        if (o.childNodes.length === 0) {
                            delete o.childNodes;
                        }
                    }
                    break;
                case 2:
                    //attribute node
                    o[node.nodeName] = node.nodeValue; //return an attribute object
                    break;
                case 3:
                    //text node
                    //strip empty nodes
                    if (node.nodeValue.match(/[^\s]/) && (strip === true)) {
                        o = node.nodeValue;
                    } else {
                        o = null;
                    }
                    if (strip !== true) {
                        o = node.nodeValue;
                    }
                    break;
                case 4:
                    //cdata section node
                    o = node.nodeValue;
                    break;
                case 9:
                    //document node;
                    o = traverse(node.firstChild);
                    break;
                case 10:
                    o = traverse(node.nextSibling);
                    break;
                }
            }
            return o;
        };
        obj = traverse(xml);
        return obj;
    };
    
        2
  •  0
  •   General Grievance Hemaka Raveen    5 年前
    function btn2Click() {
        try{
            ConObj = CreateXMLObj();
            ConObj.onreadystatechange = function () {
            if (this.readyState == 4 && this.status == 200) {            
                xmlObj = this.responseXML;
                oTitle = xmlObj.getElementsByTagName('TITLE');
                oYear = xmlObj.getElementsByTagName('YEAR');
                txt = "<center><table border=2>"; 
                for (i = 0; i < oTitle.length ; i++) {
                    txt += "<TR><TD>" + setNA(oTitle[i].childNodes) + "</TD><TD>" + setNA(oYear[i].childNodes) + "</TD> </TR>";
                                  }
                  document.getElementById("demo").innerHTML = txt; alert('stage1');
            }   
            
        };
        ConObj.open("GET", "AjaxManager.aspx?cmd=ajaxXML", true);
        ConObj.send();
        } catch (e) {
            alert(e.toString);
        }
    }
    
        
    
    function setNA(value) {
        name = "NA";
        if (value.length > 0)
            name = value[0].nodeValue;
        return name;
    }