tags:

views:

611

answers:

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"&gt;
    <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
}
+7  A: 

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

That worked! Thank-you very much!

TERACytE
A: 

Saved me too! Thank you.

Chris