简体   繁体   中英

AS3 decoding XML file

I need some help decoding my XML file. It looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<design>
    <images>
        <cell id="fill" file="cellImages/cellFill"/>
        <cell id="top" file="cellImages/cellTop"/>
        <cell id="topLeft" file="cellImages/cellTopLeft"/>
        <cell id="topRight" file="cellImages/cellTopRight"/>
        <cell id="bottom" file="cellImages/cellBottom"/>
        <cell id="bottomLeft" file="cellImages/cellBottomLeft"/>
        <cell id="bottomRight" file="cellImages/cellBottomRIght"/>
    </images>
</design> 

and this is my code:

    function xmlLoaded(event:Event):void 
    { 
        _structXML = XML(_structLoader.data); 
        trace("Data loaded." + _structXML); 

        var a:XML;
        for each( a in _structXML.images.cell)
        {
            trace("test=" + a);
        }
    }       

all it traces is the XML and 7 "test=" no data is traced from the XML.

Please help :)

It traces empty strings for all a values because they're empty XML nodes (with no content but only attributes). Using a.@file should get you the image's file.

use this:

trace("test id = " + a.attribute("id"));
trace("test file = " + a.attribute("file"));

Your use of "test=" + a is converting the a XML object to a string, and per the rules of XML toString() :

  • If the XML object has simple content, toString() returns the String contents of the XML object with the following stripped out: the start tag, attributes, namespace declarations, and end tag.

  • If the XML object has complex content, toString() returns an XML encoded String representing the entire XML object, including the start tag, attributes, namespace declarations, and end tag.

Because your <cell> nodes has "simple" content (no child nodes), it strips out the XML start and end tag from the output, and because the <cell> nodes contain nothing, you get an empty string, which doesn't output anything visible in your trace statement.

You can use toXMLString() to get the full string representation of the XML (ie the second behavior listed above regardless if the node content is "simple" or "complex").

trace("test=", a.toXMLString());

Or you could output the attributes:

trace(a.@id, "=", a.@file);

(Note: trace can take multiple arguments, you don't need to use string concatenation with + .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM