3

I am having difficultly parsing an XML file using e4X. I can acquire information from the 'version' tag, but I cannot from any nested tags.

Could someone please point out what I am doing wrong?

Here is the XML:

<NameOfRoot xmlns="http://www.theaddress.com/file">
    <version>1.0</version>
    <NameOfChild1>
        <NameOfChild2>
            <GeneralData>
                <Identifier>2678</Identifier>
            </GeneralData>
        </NameOfChild2>
    </NameOfChild1>
</NameOfRoot>

Here is the code:

<mx:HTTPService id="MyService" url="data.xml" result="resultHandler(event)" resultFormat="e4x"/>

private function resultHandler(event:ResultEvent):void {

    XMLData = event.result as XML;

    var ver:String = XMLData.*::version; // ver = 1.0
    var id:String = XMLData.*::NameOfChild1.NameofChild2.GeneralData.Identifier; //empty string
}

1 Answers1

11

Each element is namespaced in your default namespace, so you need to qualify each level:

var id:String = XMLData.*::NameOfChild1.*::NameOfChild2.*::GeneralData.*::Identifier;
// or
var n:Namespace = XMLData.namespace();
var id:String = XMLData.n::NameOfChild.n::NameOfChild2.n::GeneralData.n::Identifier;

You can set a default namespace with a "default xml namespace" directive:

default xml namespace = new Namespace("http://www.theaddress.com/file");
var id:String = xml.NameOfChild1.NameOfChild2.GeneralData.Identifier;
Michael Brewer-Davis
  • 14,018
  • 5
  • 37
  • 49
  • What if the name of the child is a reserved word e.g. const? – Strudel Jan 24 '11 at 08:59
  • You can use the longer form: parent.child("const"). See the documentation of the actionscript XML object: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/XML.html?filter_flex=4.1&filter_flashplayer=10.1&filter_air=2 – Michael Brewer-Davis Jan 24 '11 at 17:04