0

I have an XML file (simplified below) and I am unable to read the contents of the nodes:

<List xmlns="Default" xmlns:st="ST" xmlns:lc="LC">
  <Main>
    <st:ID>10</st:ID>
    <Info>
        <st:One>One</st:One>
        <st:Two>Two</st:Two>
        <st:Three>Three</st:Three>
    </Info>
  </Main>
</List>

If I try: trace(xmlOne.Main.Info.One.toString()); it does not return anything. I know it has something to do with the namespaces however I do not create the XML and cannot remove them. I tried:

namespace theDefault = "Default";
use namespace theDefault;

but no luck actually reading the node contents. So without altering the XML what is the best way to read the contents of the nodes?

4

1 回答 1

0

You must use Namespace objects to access nodes under a namespace. I made a quick example using your XML here:

var message:XML = <List xmlns="Default" xmlns:st="ST" xmlns:lc="LC">
  <Main>
    <st:ID>10</st:ID>
    <Info>
        <st:One>One</st:One>
        <st:Two>Two</st:Two>
        <st:Three>Three</st:Three>
    </Info>
  </Main>
</List>;

// First create the namespace objects for the namespaces used in the XML.
var defaultNS:Namespace = new Namespace('Default');
var stNS:Namespace = new Namespace('ST');

// Find and trace the value of the "One" node.
trace(message.defaultNS::Main.defaultNS::Info.stNS::One);

You need to prefix the E4X node name with the namespace. To access the ID node you would do:

trace(message.defaultNS::Main.stNS::ID);

And to get the whole Info node you might do:

var info:XMLList = message.defaultNS::Main.defaultNS::Info;
trace(info);

For more info on XML namespace in AS3 check out the section on Namespaces in Senocular's XML tutorial

于 2013-06-05T00:18:55.583 回答