views:

51

answers:

1

I want to be able to do something like

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

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 decedents is out of question. :(

So it's basically the ability to call xml quarry 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:

http://stackoverflow.com/questions/3699183/updating-an-actionscript-xml-object-directly-in-one-line-using-e4x/3700289#3700289

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  A: 

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].
alxx
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.
Sworup Shakya
If answer helps, don't hesitate to accept it... You get something for it :)
alxx