简体   繁体   中英

How to parse this chunk of XML in Actionscript 3

So i have this chunk of XML:

<?xml version="1.0" encoding="UTF-8"?>
<groups>
    <group>
        <image name="afp.jpg"/>
        <image name="agfa.jpg"/>
        <image name="americomp.jpg"/>
    </group>
    <group>
        <image name="canon.jpg"/>
        <image name="carestream.jpg"/>
        <image name="cmt.jpg" />
    </group>
    <group>
        <image name="ge.jpg"/>
        <image name="gtr.jpg"/>
        <image name="medlink.jpg" />
    </group>
    <group>
        <image name="pacsgear.jpg"/>
        <image name="philips.jpg"/>
        <image name="poskom.jpg" />
    </group>
    <group>
        <image name="radpro.jpg"/>
        <image name="sedecal.jpg"/>
        <image name="siemens.jpg" />
    </group>
    <group>
        <image name="sony.jpg"/>
        <image name="sotuna.jpg"/>
        <image name="sri.jpg" />
    </group>
    <group>
        <image name="tims.jpg"/>
        <image name="toshiba.jpg"/>
        <image name="ziehm.jpg" />
    </group>
    <group>
        <image name="zonare.jpg"/>
        <image name="unitedradiologysystems.jpg"/>
    </group>
</groups>

And inside the load complete event handler i have this:

var xDoc:XMLDocument = new XMLDocument();
var userXML:XML = XML(evt.target.data);
trace(userXML);
xDoc.parseXML(userXML.toXMLString());

I have a Group object defined that will store just a list of image names in an array. How do use the xDoc object to read in this data?

Why use the xDoc variable? XML is a native datatype in Flex that is great with E4X syntax as shown below (note: some error handling and null checking would probably be a good idea).

var images:XMLList = userXML.group.image as XMLList;
for each(var img:XML in images)
{
    trace(img.@name);
}

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