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!

, , ,

About James

James is a Senior New Media Developer at MMT Digital, and has BA(Hons) in Design for Interactive Media from the University of Gloucestershire. He loves designing and producing all sorts of website and Flash-related things, as well as prattling on about technologies.Day-to-day he works with Flash, Dreamweaver, Director, Microsoft Office Sharepoint Server 2007 (MOSS) and in his spare time he mucks about in Flex and Wordpress.Follow James on Twitter.

7 Responses to “Darn them XML namespaces!”

  1. Jordi Boggiano 1st December, 2008 at 11:58 am # Reply

    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.

  2. Richard Butler 1st December, 2008 at 2:10 pm # Reply

    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”

  3. Richard Butler 1st December, 2008 at 2:13 pm # Reply

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

  4. James 1st December, 2008 at 3:01 pm # Reply

    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.

  5. Nick Schneble 1st December, 2008 at 5:55 pm # Reply

    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!

  6. James 1st December, 2008 at 6:02 pm # Reply

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

  7. Conu Adela 2nd June, 2011 at 10:04 am # Reply

    If you are struggling with xml namespaces, there is a great tutorial on xpath namespaces at xml reports. It will be helpful and easy to look into.
    Here is the link:
    http://www.xml-reports.com/2011/05/xml-namespaces-for-dummies-part-1.html

Leave a Reply

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