package com.fylib.util
{
/**
* @author Frost.Yen
* @E-mail 871979853@qq.com
* @create 2015-6-18 下午2:06:01
*
*/
public class XML2JSON
{
private static var _arrays:Array;
public function XML2JSON()
{ }
public static function parse(node:*):Object {
var obj:Object = {};
var numOfChilds:int = node.children().length();
for(var i:int = 0; i<numOfChilds; i++) {
var childNode:XML = node.children()[i] as XML;
var childNodeName:String = childNode.name();
var value:*;
if(childNode.children().length() == 1 && childNode.children()[0].name() == null) {
if(childNode.attributes().length() > 0) {
value = {
_content: childNode.children()[0].toString()
};
var numOfAttributes:int = childNode.attributes().length();
for(var j:int=0; j<numOfAttributes; j++) {
value[childNode.attributes()[j].name().toString()] = childNode.attributes()[j].toString();
}
} else {
value = childNode.children()[0].toString();
}
} else {
value = parse(childNode);
}
if(obj[childNodeName]) {
if(getTypeof(obj[childNodeName]) == "array") {
obj[childNodeName].push(value);
} else {
obj[childNodeName] = [obj[childNodeName], value];
}
} else if(isArray(childNodeName)) {
obj[childNodeName] = [value];
} else {
obj[childNodeName] = value;
}
}
numOfAttributes = node.attributes().length();
for(i=0; i<numOfAttributes; i++) {
obj[node.attributes()[i].name().toString()] = node.attributes()[i].toString();
}
if(numOfChilds == 0) {
if(numOfAttributes == 0) {
obj = "";
} else {
obj._content = "";
}
}
return obj;
}
public static function get arrays():Array {
if(!_arrays) {
_arrays = [];
}
return _arrays;
}
public static function set arrays(a:Array):void {
_arrays = a;
}
private static function isArray(nodeName:String):Boolean {
var numOfArrays:int = _arrays ? _arrays.length : 0;
for(var i:int=0; i<numOfArrays; i++) {
if(nodeName == _arrays[i]) {
return true;
}
}
return false;
}
private static function getTypeof(o:*):String {
if(typeof(o) == "object") {
if(o.length == null) {
return "object";
} else if(typeof(o.length) == "number") {
return "array";
} else {
return "object";
}
} else {
return typeof(o);
}
} public static function printJSON(o:Object):void {
//trace("Debug.printJSON");
//trace(parseJSON(o));
}
public static function parseJSON(o:*, spaces:int = 1):String {
var str:String = "";
if(getTypeof(o) == "object") {
str += "{\n";
for(var i:* in o) {
str += getSpaces(spaces) + i + "=";
if(getTypeof(o[i]) == "object" || getTypeof(o[i]) == "array") {
str += parseJSON(o[i], spaces + 1) + "\n";
} else {
var type:String = getTypeof(o[i]);
if(type == "string") {
str += "\"" + o[i] + "\"\n";
} else if(type == "number") {
str += o[i] + "\n";
}
}
}
str += getSpaces(spaces - 1 < 0 ? 0 : spaces - 1) + "}";
} else if(getTypeof(o) == "array") {
str += "[\n";
var n:int = o.length;
for(i=0; i<n; i++) {
str += getSpaces(spaces) + "[" + i + "]=";
if(getTypeof(o[i]) == "object" || getTypeof(o[i]) == "array") {
str += parseJSON(o[i], spaces + 1) + "\n";
} else {
type = getTypeof(o[i]);
if(type == "string") {
str += "\"" + o[i] + "\"";
} else if(type == "number") {
str += o[i];
}
str += "\n";
}
}
str += getSpaces(spaces - 1 < 0 ? 0 : spaces - 1) + "]";
}
return str;
}
private static function getSpaces(n:int):String {
var str:String = "";
for(var i:int=0; i<n; i++) {
str += " ";
}
return str;
}
// private static function getTypeof(o:*):String {
// return typeof(o) == "object" ? (o.length == null ? "object" : "array") : typeof(o);
// }
}
}