Flex读写XML的方法总结

Flex读写XML文件
Entry posted Apr 09 by HuShaohua, last edited Apr 15
1049 Views,  0 Comments
Title:Flex读写XML文件Entry:Flex 读取xml文件的方法:

方法一:

private function readXML1():void{
    var xmlFile:File = new File("file path");
    var stream:FileStream = new FileStream();
    stream.open(xmlFile, FileMode.READ);
    var xml:XML = XML(stream.readUTFBytes(stream.bytesAvailable));
    stream.close();
}

方法二:

private function  readXML2():void{
    var urlRequest:URLRequest = new URLRequest("xml file path");
    var urlLoader:URLLoader = new URLLoader(urlRequest);
    urlLoader.addEventListener(Event.COMPLETE,completeHandler);
}
  
private function completeHandler(event:Event):void{
    var xml:XML = new XML(event.target.data);
    trace(xml);
}

Flex写XML的方法:

public static function writeXMLFile(xml:XML):void{
    var stream:FileStream = new FileStream();
      var xmlFile:File = new File("file path");
      var outputString:String = '<?xml version="1.0" encoding="utf-8"?>\n';
    outputString += xml.toXMLString();
    outputString = outputString.replace(/\n/g, File.lineEnding);
    stream = new FileStream();
    stream.open(xmlFile, FileMode.WRITE);
    stream.writeUTFBytes(outputString);
    stream.close();
}

你可能感兴趣的:(xml,Flex)