简体   繁体   English

AS3 解码 XML 文件

[英]AS3 decoding XML file

I need some help decoding my XML file.我需要一些帮助来解码我的 XML 文件。 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.它跟踪的只是 XML 和 7 个“测试=”没有从 XML 跟踪任何数据。

Please help :)请帮忙 :)

It traces empty strings for all a values because they're empty XML nodes (with no content but only attributes).它跟踪所有a值的空字符串,因为它们是空的 XML 节点(没有内容,只有属性)。 Using a.@file should get you the image's file.使用a.@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() :您对"test=" + a是将a XML 对象转换为字符串,并按照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.如果 XML 对象具有简单内容,则toString()返回 XML 对象的 String 内容,并去掉以下内容:开始标记、属性、命名空间声明和结束标记。

  • 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.如果 XML 对象具有复杂的内容,则toString()返回表示整个 XML 对象的 XML 编码字符串,包括开始标记、属性、命名空间声明和结束标记。

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.因为您的<cell>节点具有“简单”的内容(没有子节点),所以它从输出中去除了 XML 开始和结束标记,并且因为<cell>节点不包含任何内容,所以您会得到一个空字符串,它不会输出trace语句中可见的任何内容。

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").您可以使用toXMLString()获取 XML 的完整字符串表示(即上面列出的第二个行为,无论节点内容是“简单”还是“复杂”)。

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 + .) (注意: trace可以接受多个参数,您不需要使用字符串连接+ 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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