Question

I want to be able to do something like

var XMLquery:String = "a1.a2.a3";
var parserVal:XML = parserMethod(XMLquery);
// or
var parserVal:XMLList = parserMethod(XMLquery);`

and get an output something like

<a3>Some value</a3>

Important: And I want to be able to replace the output at a1.a2.a3 so using descendants is out of question. :(

So it's basically the ability to call xml query in string. Is there a way to do this. Just a hint would be super, I can do it if I got a bit of head-start.

Thank you!


I think I found a solution with the help from this link:

Updating an actionscript xml object directly in one line using e4x?

public static function updateXml(xml:XML, path:String, data:XMLList = null,update:Boolean = false,XmlListOnly:Boolean = false):* {
            var nodesArray:Array = path.split(".");
            var tempXML:XML = xml;
            var tempXMLCandidate:XML;
            var tagName:String;
            for (var i:int = 0; i < nodesArray.length; i++){
                tagName = nodesArray[i];
                if (i == nodesArray.length - 1){
                    if (data != null && update && !XmlListOnly){
                    tempXML[tagName] = data;
                    }else if (XmlListOnly){
                        return tempXML[tagName];
                    }else{
                    return tempXML[tagName].length();
                    }
                }else{
                    tempXMLCandidate = tempXML[tagName][0];
                    if (!tempXMLCandidate){
                        tempXML.appendChild(tempXMLCandidate);
                    }
                tempXML = tempXMLCandidate;
                }
            }
            return tempXML;
        }

You can call it like this:

updateXml(xmlHold, "words.exercise", sortedXmlList, true);
Was it helpful?

Solution

I'm too lazy to code and test it, but here's idea:

  • Break your query on parts "a1.a2.a3".split(".")
  • Go on the parts, calling xml.elements(parts[i]) (you'll need extra (maybe nested) function for recursive calls)
  • If you get non-empty XMLList, repeat calling elements on that list using next part.
  • On last part, extract text from it with children()[0].
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top