reading XML attributes in javascript

后端 未结 2 1458
囚心锁ツ
囚心锁ツ 2021-01-24 16:22

Guess it\'s a simple question for a javascript guru, but i\'m learning and got a problem I can\'t guess out.

I\'m interested in reading an XML file using javascript. He

相关标签:
2条回答
  • 2021-01-24 16:25

    In, this case, since the attribute elements you are looking for are direct children of the object element, a simple thing you can do is iterate through the child elements of the object element by hand:

    var obj = objList[0]
    var childNodes = obj.childNodes
    for(var i=0; i<childNodes.length; i++){
        var child = childNodes[i];
        if(child.nodeType == 1 && child.nodeName == 'attribute'){
            do_something(child);
        }
    }
    

    For information on these and other DOM methods, I recommend checking out the documentation over on MDN.

    0 讨论(0)
  • 2021-01-24 16:32

    The problem you are running into is that technically "attributes" of subobject are also children of object. Remember that any element is a Node object, and on that Node you can get all childNodes. You can do that and process each "Element" node and determine if it is an "attribute" Node.

    See http://www.w3schools.com/jsref/dom_obj_node.asp for more information on this.

    0 讨论(0)
提交回复
热议问题