Question

Given the following sample XML (here I am showing hard-coded, but normally I load from an external file):

var myXML:XML = new XML('
    <xmlout xmlns:ns1="http://some.namespace.com/ns1" xmlns:ns2="http://some.namespace.com/ns2" xmlns:ns3="http://some.namespace.com/ns3">
        <data>
            <item>
                <ns1:id>some_id</ns1:id>
            </item>
        </data>
    </xmlout>');

I am storing the namespaces as namespace objects in an object, like:

var xmlNamespaces:Object = {};

for (var i:uint = 0; i < myXML.namespaceDeclarations().length; i++) {
    var ns:Namespace = myXML.namespaceDeclarations()[i]; 
    xmlNamespaces[ns.prefix] = new Namespace(ns.prefix, ns.uri);
}

I am trying to do something like:

trace(myXML.data.item.xmlNamespaces["ns1"]::id.value);

Any idea if this is possible? I have not had any success. Thanks!

Edit: I should note that I can do this, with no problem, using:

default xml namespace = xmlNamespaces.ns1;
var id:String = myXML.data.item.id;

I am specifically trying to figure out how to eliminate having to set the default namespace every time...

Était-ce utile?

La solution

Sorry this has been taking me so long to get back to you as I am sure you probably needed it yesterday.
I was on the road for 8 hours today.
Writing inline XML requires the use of a single root node to define the body of the XML document.
Your first issue is you wrapped everything with a single quote. Take it out the parameter is not a string.
Your other issue as stated above you need to wrap it with a root tag.
This code compiles fine in flash cs4 let me know if you still can't get that to work.
Notice in the code below I never reference the root node.

var myXML:XML = new XML(
    <root>
        <xmlout xmlns:ns1="http://some.namespace.com/ns1" xmlns:ns2="http://some.namespace.com/ns2" xmlns:ns3="http://some.namespace.com/ns3">
            <data>
                <item>
                    <ns1:id>some_id</ns1:id>
                </item>
            </data>
        </xmlout>
    </root>);


var xmlNamespaces:Object = {};

for (var i:uint = 0; i < myXML.xmlout.namespaceDeclarations().length; i++) {
    var ns:Namespace = myXML.xmlout.namespaceDeclarations()[i]; 
    trace(ns.prefix + ' ' + ns.uri)
    xmlNamespaces[ns.prefix] = new Namespace(ns.prefix, ns.uri);
}
// output is 
// ns1 http://some.namespace.com/ns1
// ns2 http://some.namespace.com/ns2
// ns3 http://some.namespace.com/ns3
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top