简体   繁体   English

将xml导入xml并使用ActionScript 3阅读

[英]Import xml into a xml and read it with actionscript 3

I want to import a xml into an xml and read it with ActionScript 3. A single imported xml-File is possible to import but I have a big problem using it that way I want to. 我想将xml导入xml并使用ActionScript 3读取它。可以导入单个导入的xml文件,但是我想以这种方式使用它存在很大的问题。

So my first xml-File looks that way: 所以我的第一个xml文件看起来是这样的:

<!DOCTYPE doc [
<!ENTITY bonuses SYSTEM "bonuses.xml">
]>    
<mission>
            ...
            <wealth money="1000" />
            <bonuses>bonus</bonuses>
            <bonuses>
                <first>&bonuses;</first>
                <second>&bonuses;</second>
            </bonuses>
        </mission>

My Second file looks that way: 我的第二个文件是这样的:

<?xml version="1.0" encoding="utf-8"?>
<bananas>
    <descr>Banana Description</descr>
    <impact>You gain more gold!</impact>
    <bonus>15</bonus>
</bananas>

I'm trying to access the xml file with this function: 我正在尝试使用此功能访问xml文件:

private function xmlLoaded(e:Event):void 
{
    _xml = new XML(e.target.data);
    trace("XML LOADER: XML LOADED CORRECTLY");
// Correct Output: Bonus
        trace("XML LOADER: BONUS 0: " + _xml.mission[0].bonuses[0]); 
// Incorrect Output: Nothing!
    trace("XML LOADER: BONUS 1: " + _xml.mission[0].bonuses[1].first.descr);
    _stage.dispatchEvent(new Event("completed"));
}

Like I said this works fine for the first loaded xml file but not for the imported. 就像我说的那样,这对于第一个加载的xml文件很好,但对于导入的文件却不行。 Are there any soultions or is it a restriction of actionscript? 是否有任何遗忘或限制动作脚本?

Thanks a lot! 非常感谢!

Greetings Michael 迈克尔的问候

First of all, don't use XML , use JSON . 首先,不要使用XML ,而要使用JSON Actionscript 3 has a native JSON parser, which is faster than the default XML parser. Actionscript 3具有本机JSON解析器,它比默认XML解析器快。 JSON supports arrays, it's easier to read and 50% to 66% in size of the same data in XML . JSON支持数组,它更易于读取,并且XML相同数据的大小为50%到66%。 Actionscript 3 converts a JSON string into an Object with properties which eliminates the need of stuff like _xml.mission[0].bonuses[1].first.descr . Actionscript 3将JSON字符串转换为具有属性的Object ,从而消除了诸如_xml.mission[0].bonuses[1].first.descr

Once you switch to JSON , problems like yours just fly away on the back of butterflies twinkle, twinkle . 切换到JSON ,像您这样的问题就在蝴蝶闪烁,闪烁的背面飞走了。

If you DO have to use an XML, then get yourself or make yourself an XML to Object converter, which is something you can easily debug. 如果您必须使用XML,则可以使用XML或Object转换为XML,这很容易调试。 Convert attributes to properties, child nodes into an array with said children, etc. 将属性转换为属性,将子节点转换为具有所述子节点的数组等。

Well, there are many bizarre things in your code. 好吧,您的代码中有许多奇怪的事情。

1) Are you loading both XMLs into the same object _xml ? 1)您是否将两个XML都加载到同一对象_xml You should have a different object for each XML. 每个XML应该有一个不同的对象。 When you do the following code, you are removing what's inside _xml and putting something new. 当您执行以下代码时,您将删除_xml并放入新内容。

_xml = new XML(e.target.data);

2) Of course your second trace will never work, even if you had 2 XML objects, because it's not using the nodes correctly... it should be simply this. 2)当然,即使您有2个XML对象,第二条跟踪也永远不会起作用,因为它没有正确使用节点……这应该就是这样。

trace(_xml.descr);

If you need to combine the 2 XMLs into one XMl object, you need to do that after loading both XMLs. 如果需要将两个XML组合到一个XMl对象中,则需要加载两个XML 之后执行此操作。

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

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