1

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);

1 Answer 1

3

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].
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah! I did pretty much the same as you suggested. But twisted the code a bit to match my need. I've posted the code above, haven't got the time to check it properly. Thank You for your help alxx! Really appreciate it.
If answer helps, don't hesitate to accept it... You get something for it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.