1 Star2 Stars3 Stars4 Stars5 Stars
Loading ... Loading ...

Darn them XML namespaces!

Ugh, this E4X stuff is all a bit new to me – I’m still not used to namespaces and stuff.  For some reason I’ve had trouble extracting data from XML when I’m using a namespace.  I have no idea why, but it seemed that with the namespace in use, my attempts at extracting data from the XML returned blank data.

Never fear!  It seems that either removing the namespace or replacing it with a wildcard would let me access the data.  Kudos to the blog posts of Riley (Got RIA?) : Remove Annoying XML Namespaces in Flex / AS3 and Getting around unknown namespaces in Flex / AS3.

So until I get my head around how namespaces work, I’m going to be using the techniques described in the links above.  Until then, if anyone can explain this to me – I’d be very greatful!

6 Comments »

  1. Just had the same problem and a colleague gave me the solution, when you know what namespace to expect at least.

    class Foo {
    namespace blah = “http://url_of_your_xmlns_declaration”;
    use namespace blah;
    }

    If you set up your class like this, with the url used for the namespace in the xml file, and that use statement using it, you can then access everything properly.

    That being said, you should be careful because once you’ve namespaces, code like someXml.name() returns “http://namespaceUrl::realName”, someXml.localName() returns only “realName” though.

    Comment by Jordi Boggiano — 1 December 2008 #

  2. A handy one if your XML only has one namespace is the following:

    default xml namespace = “http://my.xml.com/ns/1.0″;

    var x : XML =

    Test

    ;

    trace( x.node.item.text() ); // traces “Test”

    Comment by Richard Butler — 1 December 2008 #

  3. Doesn’t look like your comment system likes XML, but hopefully you can see what I was getting at!

    Comment by Richard Butler — 1 December 2008 #

  4. Aha, Akismet was eating the comments…

    Namespaces is something I’ll have to look into a bit more. I’ll give both of those suggestions a shot though, and see what I can make work…

    I was trying to extract the paths of the icons from the applicationDescriptor files in an AIR application (for my ApplicationInfo class) but was getting stumped.

    Comment by James — 1 December 2008 #

  5. James,

    You can easily handle multiple namespaces in XML using E4X without changing the namespace used in the class (per Jordi’s comment).

    I wrote up a post on it a while back:
    http://userflex.wordpress.com/2008/04/03/xml-ns-e4x/

    This technique can also be applied when you don’t know what namespaces are specified beforehand, since you can iterate over the XML.namespaceDeclarations() array to determine what ones are used in the XML.

    Hope that helps!

    Comment by Nick Schneble — 1 December 2008 #

  6. Thanks Nick – your post makes a lot of sense, my favourate solution so far!

    Comment by James — 1 December 2008 #

Leave a comment

XHTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Note: This post is over a year old. You may want to check later in this blog to see if there is new information relevant to your comment.