我试图更新XML文件并保存已保存的根元素的元素。
我只找到其中的XML文件只是打开并没有保存的例子。 谁能帮我找到保存结果的最佳方式?
我的项目现在看起来是这样的:
我加载通过的URLLoader /的URLRequest一个XML文件,并在多个文本字段显示内容。 从输入领域的新文本保存(追加),通过直接的FileStream在applicationStorageDirectory XML文件(这将是在iPhone上)。
然后新的输入应该被添加到屏幕上的名单(有一个for循环创建)并显示,但是,它不能得到那么远。 从XML文件中读取新保存输入后,因为XML文件是没有很好地形成了我自然会得到错误1088。
这是因为输入是根元素后所附,结果如下所示:
<root>
<message></message>
<date></date>
</root>
<message>new input</message>
<date></date>
当然,当我想是这样的:
<root>
<message></message>
<date></date>
<message>new input</message>
<date></date>
</root>
但我不知道我怎样才能做到这一点。
我几次试图避免追加,像在加载XML内容,改变它,然后重新写的一切。 但由于我还是新的AS3我无法得到它的工作。
这将是巨大的,如果有人能告诉我怎么去会,也许是最好的方式如何解决它。
好吧,只是想我会张贴我在迄今为止的地方...更新
这应该是能够输出XML对于大多数对象,我只是在它扔了几个测试用例到目前为止,如果你想开始尝试这种调试,让我知道,如果您有任何遗漏部分或坏的XML输出让我知道...我会继续和尝试,并在解析器努力扭转过程中由该版本生成的,当我这样做可能会改变XML。
package
{
import flash.utils.describeType;
import mx.collections.IList;
public class AS3ToXMLMapper
{
public function AS3ToXMLMapper()
{
}
public static function generateXML(objectToMap:Object, basePropertyName:String="root"):String
{
var describeXML:XML = describeType(objectToMap);
var xmlOutput:String = "<"+basePropertyName+" name=\""+describeXML.@name+"\" base=\""+describeXML.@base+"\">\n";
if(describeXML.@isDynamic=="true")
{
for(var property:String in objectToMap)
{
xmlOutput += "<"+property+">";
xmlOutput += objectToMap[property];
xmlOutput += "</"+property+">";
}
}
else if(objectToMap is XML)
{
xmlOutput+=(objectToMap as XML).toString();
}
else if(objectToMap is XMLList)
{
xmlOutput+=(objectToMap as XMLList).toString();
}
else
{
for each(var accessor:XML in describeXML..accessor)
{
xmlOutput+="\t"+exportProperty(objectToMap, accessor,true);
}
for each(var variable:XML in describeXML..variable)
{
xmlOutput+="\t"+exportProperty(objectToMap, variable, false);
}
}
xmlOutput += "</"+basePropertyName+">\n";
trace(xmlOutput);
return xmlOutput;
}
private static function exportProperty(objectToMap:Object, xmlObj:XML, isAccessor:Boolean):String
{
var xmlOutput:String="";
var propName:String = xmlObj.@name.toString();
var objectValue:Object = objectToMap[propName];
if(!objectValue)
{
xmlOutput += "<"+propName+">";
xmlOutput += "</"+propName+">";
return xmlOutput;
}
if(isAccessor && xmlObj.@access != "readwrite")
{
return "";
}
if(objectValue is Array)
{
return exportArray(objectValue as Array, xmlObj.@name);
}
else if(objectValue is IList)
{
return exportArray((objectValue as IList).toArray(), propName);
}
else if(objectValue is int || objectValue is Number || objectValue is String || objectValue is uint || objectValue is Boolean)
{
xmlOutput += "<"+propName+" type=\""+xmlObj.@type+"\">";
xmlOutput += objectValue;
xmlOutput += "</"+propName+">";
}
else
{
return generateXML(objectValue, propName);
}
return xmlOutput;
}
private static function exportArray(array:Array, arrayName:String):String
{
var xmlOutput:String = "<"+arrayName+">\n";
for each(var element:Object in array)
{
xmlOutput+="\t"+generateXML(element,"arrayElement");
}
xmlOutput += "</"+arrayName+">\n";
return xmlOutput;
}
}
}
用法是这样的:
var fs:FileStream = new FileStream();
fs.open(new File("C:\\test.xml"),FileMode.WRITE);
var thingToExport:Object = {aProperty:"someValue"};
var as3XMLMapper:String = AS3ToXMLMapper.generateXML(thingToExport);
fs.writeUTFBytes(as3XMLMapper);
fs.close();
那岂不是更好只需用XML文本工作?
package
{
import flash.display.Sprite;
public class ZutAlors extends Sprite
{
public function ZutAlors()
{
trace(messagesToXMLString('hello world'.split(' ')));
}
private function messageToXML(m:String, d:Date = null):XMLList
{
return <ret>
<message>{m}</message>
<date>{(d || new Date()).toString()}</date>
</ret>.children();
}
private function messagesToXMLString(array:Array):XML
{
const ret:XML = <root />
for each(var s:String in array)
{
ret.appendChild(messageToXML(s));
}
return ret;
}
}
}
你得到一个编译错误,如果XML不是简洁(wellformed)...