AS3:将二进制XML加载到Flash

时间:2014-08-12 01:57:18

标签: xml actionscript-3 flash

我将所有数据从XML文件传输到Flash。 XML文件中的所有数据都将显示在Flash中。

我的代码在flash中:

xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("salesAchievementXML.xml"));

function LoadXML(e:Event):void 
{
    xmlData = new XML(e.target.data);
    LoadSales(xmlData);
}

function LoadSales(imageinput:XML):void 
{
    var _salesInfo:XMLList = imageinput.SalesAchievement; 
    var _salesDetails:XMLList = _salesInfo;

    var _salesImage:XMLList  = _salesDetails.fldUserImage;
    ncounter=0;
    for each (var _salesImageElement:XML in _salesImage) 
    {
        _ArrSalesImage[ncounter] = _salesImageElement;
        ncounter++;
    }
}

示例XML:[我只是截图,因为二进制文件很长]。 enter image description here

HEre是我要使用的模板: enter image description here

所以我的问题是: 这是来自webservice的二进制类型。然后我打算在图像框中显示。但问题是,有没有办法将二进制图像显示为闪存或应该转换?如果转换,请帮我如何转换它..

谢谢!

1 个答案:

答案 0 :(得分:1)

要解码base64字符串,您需要某种加密库。

或者,只需稍加更改即可使用decoder from Flex

从FLA的根目录中,将此文件放在'mx / utils'文件夹中,如:

  

folder-structure

mx.utils.Base64Decoder.as (已更改为与Flash纯ActionScript一起使用)

////////////////////////////////////////////////////////////////////////////////
//
//  Licensed to the Apache Software Foundation (ASF) under one or more
//  contributor license agreements.  See the NOTICE file distributed with
//  this work for additional information regarding copyright ownership.
//  The ASF licenses this file to You under the Apache License, Version 2.0
//  (the "License"); you may not use this file except in compliance with
//  the License.  You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////

package mx.utils
{

import flash.utils.ByteArray;

/**
 * A utility class to decode a Base64 encoded String to a ByteArray.
 *  
 *  @langversion 3.0
 *  @playerversion Flash 9
 *  @playerversion AIR 1.1
 *  @productversion Flex 3
 */
public class Base64Decoder
{
    //--------------------------------------------------------------------------
    //
    //  Constructor
    //
    //--------------------------------------------------------------------------

    /**
     * Constructor.
     *  
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    public function Base64Decoder()
    {
        super();
        data = new ByteArray();
    }

    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------

    /**
     * Decodes a Base64 encoded String and adds the result to an internal
     * buffer. Strings must be in ASCII format. 
     * 
     * <p>Subsequent calls to this method add on to the internal
     * buffer. After all data have been encoded, call <code>toByteArray()</code>
     * to obtain a decoded <code>flash.utils.ByteArray</code>.</p>
     * 
     * @param encoded The Base64 encoded String to decode.
     *  
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    public function decode(encoded:String):void
    {
        for (var i:uint = 0; i < encoded.length; ++i)
        {
            var c:Number = encoded.charCodeAt(i);

            if (c == ESCAPE_CHAR_CODE)
                work[count++] = -1;
            else if (inverse[c] != 64)
                work[count++] = inverse[c];
            else
                continue;

            if (count == 4)
            {
                count = 0;
                data.writeByte((work[0] << 2) | ((work[1] & 0xFF) >> 4));
                filled++;

                if (work[2] == -1)
                    break;

                data.writeByte((work[1] << 4) | ((work[2] & 0xFF) >> 2));
                filled++;

                if (work[3] == -1)
                    break;

                data.writeByte((work[2] << 6) | work[3]);
                filled++;
            }
        }
    }

    /**
     * @private
     */
    public function drain():ByteArray
    {
        var result:ByteArray = new ByteArray();

        var oldPosition:uint = data.position;    
        data.position = 0;  // technically, shouldn't need to set this, but carrying over from previous implementation
        result.writeBytes(data, 0, data.length);        
        data.position = oldPosition;
        result.position = 0;

        filled = 0;
        return result;
    }

    /**
     * @private
     */
    public function flush():ByteArray
    {
        if (count > 0)
        {
            throw new Error();
        }
        return drain();
    }

    /**
     * Clears all buffers and resets the decoder to its initial state.
     *  
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    public function reset():void
    {
        data = new ByteArray();
        count = 0;
        filled = 0;
    }

    /**
     * Returns the current buffer as a decoded <code>flash.utils.ByteArray</code>.
     * Note that calling this method also clears the buffer and resets the 
     * decoder to its initial state.
     * 
     * @return The decoded <code>flash.utils.ByteArray</code>.
     *  
     *  @langversion 3.0
     *  @playerversion Flash 9
     *  @playerversion AIR 1.1
     *  @productversion Flex 3
     */
    public function toByteArray():ByteArray
    {
        var result:ByteArray = flush();
        reset();
        return result;
    }

    //--------------------------------------------------------------------------
    //
    //  Private Variables
    //
    //--------------------------------------------------------------------------

    private var count:int = 0;
    private var data:ByteArray;
    private var filled:int = 0;
    private var work:Array = [0, 0, 0, 0];

    private static const ESCAPE_CHAR_CODE:Number = 61; // The '=' char

    private static const inverse:Array =
    [
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
        64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
        15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
        64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
        64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
    ];
}

}

现在,您可以解码字符串并将其转换为字节数组:

import flash.display.Loader;
import flash.utils.ByteArray;
import mx.utils.Base64Decoder;

var decoder:Base64Decoder = new Base64Decoder();
decoder.decode("iVBORw0KGgoAAAANSUhEUgAAACgAAAAoCAYAAACM/rhtAAAKqUlEQVRYhe2YWYwc13WGv1q7urt6n+nZF5IzJDWkSCoiRY1MiZToBbYUCrJgx7ADIwECI0igLDYCPSuAEyR5SWIndgzHeTJhSIptGJZs05YFm9o4EmXOkCJFzr5vvVVXd1XXngfSgRWK9JBiHgLkAPVyq07d7/73nnvOvfD/9sFMuF3H+3skKZ9JHm1vSx0z6va7a5vGs28sB/6dhAOQb9cxm058ae9w1zOdxYwmSlL00isX8ywbX72TcADSrToc6pLUu/tSX9s92PG0piqy7wd0FDOC6/kHM1Hj+elKUPvN7z8+kto5kJESsxW/fjuA4q06FDLxPxrozP1hFEVXfyAKOI5HPqsXQ8QT9/e8d8zdxcy32nKJv//s/sTt8N36FKcS6p/HNUVuWA5hGFE2AtYqBo7vk23T/85crP4UuATw8eHYtu6O3IfWNqhsrDXvLOAT+3V517bs59WYMry2YT4/caV69o3lgGrT1StzJbSYSCIuE4tJqKKEKov0FnParsHuF3evlF9dWtr8U1nT/iyf01ndqOWTqpgCzDsG2J6LPxrK6jesUJR6+9r+qqM99bXeqfKX1+uBnE7ItOeTZPQYuVSc9kKaXDaJ63o0ms7gtt7C4HJ/8cm4pmgtx8P1fIDbArxhkAx1xo96oXii3nCpW56YSscPpX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ihTXsgYdpqgpWrUmrbjNXcwgyghGEXLllwKdG9YHBod2viAK9hb6+yaX5mRnRjR0/OHpYPjv5n8pYwyKT7YEwoFiIszJXoewEpDWRvriDuV4hcAwU8zJ5e/qK7zuzob36yFRml/KyW6Y/FeKkBL9ihX+dUIXGLQE+NaoLqUzuH/q3D/euLMzOXpo4+yCwUezses5znScPDHXw1rkpqsk0ZdPHWjJYnCxRtXy0uEw2o5Hd/BGRU19rtuyfXzGCM14QOZ2pxYv51uros2WpLyH5AYLwL6mY8NXvj9vvy3EzBfu7ewce1hJJjGr5G8D6V15v8EzH6nzPwBCKItWSjXca5XJP76uTIY8VVCpNn5YX0vA8ujMylrHx9amK/yugCYwDC6tm0Fo1g1/329rVJoevzt/4WvFm5ZaSzuWVMAho2ZZyTdV8vq3jU3WjQq3pTpZr1ZOp2uve98cjHElAick0nIBQELC8gIYXVYAq8AJwAagDLmBde8LLpZvfed4wkxzuUxu5QvtDnuvs2LnnwMMjBw79Xs/A9mcEQWwrlUrlb7749j8v14MZLTSXNH9z++SGrx87kCOeiHHPcIZfTlWab6+6pxw/+u41yNuyGwKOLbnBsFY9HdPivVPvnk8360bcbjaMRr128vSvLn/xjTnrFHDRdKMLtutMRq4lbBhWerBNNc/N18xXFqyJBSP4xzBi4XbhYAvl1lOj+nVtX3n9+oArJkW252WG8zIVO2SuFvDOxm+/Ofg/b/8FrV74eebdEP8AAAAASUVORK5CYII=");

var bytes:ByteArray = decoder.toByteArray();
var loader:Loader = new Loader();
loader.loadBytes(bytes);

addChild(loader);

上面的代码产生:

screen-capture