0

the e4x implementation in as3 doesn't seem to be able to handle node names that have dashes in them. The musicbrainz api returns xml with a node named artist-list and i can't seem to get it to let me access the node.

sample from http://musicbrainz.org/ws/1/artist/?type=xml&name=dr%20dog :

<metadata xmlns="http://musicbrainz.org/ns/mmd-1.0#" xmlns:ext="http://musicbrainz.org/ns/ext-1.0#">
    <artist-list offset="0" count="1090">
        <artist type="Group" id="e9aed5e5-ed35-4244-872e-194862290295" ext:score="100">
        </artist>
    </artist-list>
</metadata>

If I try to access it like so myXml.artist-list i get the compile time error:

Error: Access of undefined property list.

Anybody know of a workaround?

--Edit: full source--

var l:URLLoader = new URLLoader();
    l.load(new URLRequest("http://musicbrainz.org/ws/1/artist/?type=xml&name=dr%20dog"));
    l.addEventListener(Event.COMPLETE, function(e:Event) {
        var myXml:XML = XML(e.target.data);
        trace(myXml.artist-list)
    });
greggreg
  • 11,945
  • 6
  • 37
  • 52

2 Answers2

3

Added a working sample using the two syntaxes : http://wonderfl.net/c/hyuG

You can use myXml["my field"] notation to get your field, as your xml have namespace in it you have to specify it, one way to do this is:

var ns:Namespace=new Namespace("http://musicbrainz.org/ns/mmd-1.0#")
trace(myXml.ns::["artist-list"])

another way is to set the default namespace:

var ns:Namespace=new Namespace("http://musicbrainz.org/ns/mmd-1.0#")
default xml namespace=ns
trace(xml["artist-list"])
Patrick
  • 15,702
  • 1
  • 39
  • 39
  • attempting to set the namespace like above gives me: `TypeError: Error #1034: Type Coercion failed: cannot convert XML@8312da9 element to Namespace.` And the code to set the default namespace throws a syntax error. – greggreg Mar 15 '11 at 21:05
  • @greg Yes my bad i have made a mistake, it's myXml.ns:: and not myXml::ns . – Patrick Mar 15 '11 at 21:07
  • finally got it to work using the default namespace. Using `xml.ns::["artist-list"].artist[0].name` throws `a term is undefined...` error. Does setting the default ns in one class effect e4x in other classes? – greggreg Mar 15 '11 at 22:12
1

You can access it with 'child'

xml.child("artist-list")

Will return an XMLList. Not as neat as regular e4x but that's the way it goes..

Roy
  • 325
  • 1
  • 6
  • I tried `myXML.child("artist-list")`, `XMLList(myXml.child("artist-list"))`, `myXML.child("artist-list").artist.length()`, and `myXML.child("artist-list").child("artist").length()` and I get nothing. – greggreg Mar 15 '11 at 21:08