1

ActionScript-3

我有一个 XML 字符串,它代表任何类型的对象(字符串和原语矩阵)。我需要从字符串中取出对象/矩阵。

// I have:
var xml: String = "<?xml version="1.0" encoding="utf-16"?><ArrayOfArrayOfAnyType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  <ArrayOfAnyType>    <anyType xsi:type="xsd:string">TEXT</anyType> ....... </anyType></ArrayOfAnyType></ArrayOfArrayOfAnyType>"

// I need:
var obj: Object = ???;
// or in the end:
var array: Array = ???;
  • 如何将字符串反序列化为 object ?
  • 如何将任何对象序列化为字符串?


更新:

  • 我查看了对象children()列表,new XML(str);我可以查看trace()这些值。所以我可以手动迭代列表以获取我的矩阵,但不能自行闪光吗?

更新 2:

  • 我没有使用 Flex
  • 我的矩阵也包含DateTime值,因此必须尽可能准确地使用类型属性。
  • 为了反序列化我得到的字符串,我做了我在第一次更新中写的。
    • 由于命名空间,我无法掌握对我来说非常重要的类型属性。
    • 所以我杀死了所有的命名空间垃圾来得到这样的东西:
    • <anyType type="string">blah blah</anyType>
      • 我操纵属性字符串来杀死"xsi:""xsd:"垃圾
    • 然后我只是迭代并建立了我想要的矩阵。
  • 虽然这个问题没有解决,但我希望有更好的方法来做到这一点。
4

1 回答 1

1

如果您使用的是 Flex,您可以使用SimpleXMLDecoder

function toObj(data:XML):Object {
    var xmlDoc:XMLDocument = new XMLDocument(data);
    var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
    return decoder.decodeXML(xmlDoc);
}

function toXML(obj:Object):XML {
    var qName:QName = new QName("root");
    var xmlDoc:XMLDocument = new XMLDocument();
    var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDoc);
    var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(obj, qName, xmlDoc);

    return new XML(xmlDoc.toString());
}

如果像我一样,您没有使用 Flex 及其 API,那么这是我编写的用于转换为 Object 的纯 AS3 方法。这仅涉及到一个对象;我从来不需要序列化为字符串。

function translateXML(xml:XML):* {
    /*  Created by Atriace: Converts XML into an Object, with nested arrays, and relevant object types.
        Useful if you find AS3's XML methods an asinine waste of time. */
    var data:XMLList = xml.children();
    var store:Object = mineAttributes(xml.attributes()), item, key:String;

    if (hasTwo(data)) { // should this be an object or array?
        for each (item in data) {  // we store arrays by the name of the array
            key = item.name();
            if (store[key] == null) {
                store[key] = new Array(); // and then the contents inside that array
            } else if (getType(store[key]) != "Array") {
                trace("store[" + key + "] = " + store[key] + ":" + getType(store[key]));
            }
            store[key].push(translateXML(item));
        }
    } else { // Assuming there were no repeating elements at the beginning, we create unique objects
        for each (item in data){ 
            key = item.name();
            if (key == null) {  // Assuming we have an encapsulated string, we add a tag called "content" with its value.
                store["content"] = item.toString();
            } else {
                store[key] = translateXML(item);  // Otherwise, we recursively loop into the nested objects.
            }
        }
    }
    return store;
}

function hasTwo(data):Boolean {
    /* Determines whether there are two of the same object name at the beginning of a list. */
    var answer:Boolean = false;
    var original;
    var i:int = 1;
    for each (var k in data) {
        if (i == 2) {
            if (original == k.name()) {
                answer = true;
            }
            break;
        } else {
            original = k.name();
        }
        i++
    }
    return answer;
}

function mineAttributes(data:XMLList):* {
    /* Returns all the attibutes of an XML node */
    var d:Object = {}, key:String, val:String;
    for each (var item in data){
        key = item.name();
        val = item;
        d[key] = val;
    }
    return d;
}

function getType(value:*):String {
    // Returns the type of object passed to it.
    var msg:String = flash.utils.getQualifiedClassName(value);
    if (msg.lastIndexOf("::") != -1) {msg = msg.split("::")[1];}
    return msg;
}
于 2013-11-05T16:55:48.203 回答