0

I'm trying to build an abstract method to get all the nodes in an XML object by node name. I don't know the structure of the XML ahead of time.

So with this code I would like to get a list of all "item" nodes and all "x" nodes:

var xml:XML = <root><items><item/><item/><item><x/><item><item><x/></item></items></root>
var nodeName:String;
var list:XMLList;


list = getNodeByName(xml, "item"); // contains no results
list = getNodeByName(xml, "x"); // contains no results

// what am i doing wrong here?
public static function getNodeByName(xml:XML, nodeName:String):XMLList {
     return xml.child(nodeName);
}
1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
  • Can you please avoid code that do not pass at compile time. Thx. This avoid waste of time. The answer of rbyte should help you. – tatactic May 18 '12 at 08:15
  • @tatactic please don't change the code if you suspect it to be wrong: the question could be based on that wrongness. If you suspect a typo in the question, just comment and ask if that might be an error in the question that is not in the origional code. – Nanne May 18 '12 at 09:04
  • I should have mentioned that was pseudo code. Thanks – 1.21 gigawatts May 18 '12 at 16:37
  • I apologize for this. I did not now exactly how to deal in this situation... This is why I note this as comment in the code. A comment describing what I noticed was surely preferable. I wanted to delete the edit but it was not possible. So, sorry to Gigawtts and all others. – tatactic May 22 '12 at 22:17

2 Answers2

3

As the name of the method says, child will only return a list of the children of the XML object matching the given parameter. if you want to get all descendants(children, grandchildren, great-grandchildren, etc.) of the XML object with a given name you should use descendants method:

public static function getNodeByName(xml:XML, nodeName:String):XMLList {
     return xml.descendants(nodeName);
}

Hope it helps.

rdleal
  • 987
  • 5
  • 15
1

Do you want all the nodes of the same name from different parents to be joined in one XML structure? In that case, what you can do is this:

    public static function getNodesByName(myXML_:XMLList, nodeName_:String) : XMLList {
        var result:XMLList = new XMLList();
        for (var i1:Number = 0; i1 < myXML_.children().length(); i1++ ) {
            if (myXML_.children()[i1].name() == nodeName_) {
                result += myXML_.children()[i1].valueOf();
            } else if (myXML_.children()[i1].children()) { 
                result += getNodesByName(XMLList(myXML_.children()[i1].valueOf()), nodeName_);
            }
        }
        return result;
    }

It will return you XMLList with all nodes that have specified name. Use it like this:

var nodesList:XMLList = getNodesByName(myXML.children(), "myNodeName").valueOf();

If you want to turn that list into XML then:

var myXMLListTurnedIntoXML:XML = XML("<xml></xml>");
myXMLListTurnedIntoXML.appendChild(getNodesByName(myXML.children(), "myNodeName").valueOf());

Hope that will help.

Łukasz Zaroda
  • 869
  • 2
  • 19
  • 55