Flex AIR —— 文件读写

一、文件内容

<funds>
  <fund>
    <name>中银中国</name>
    <lot>274</lot>
    <net>1.6612</net>
  </fund>
  <fund>
    <name>广发稳健</name>
    <lot>280.85</lot>
    <net>1.5942</net>
  </fund>
</funds>
 

 

二、源码

private var filePath:String = null;
private var xmlList:XMLList = null;

private function openConfig():void{
	var file:File = new File();
	file.browseForOpen("选择文件", [new FileFilter("*.xml","*.xml")]);
	file.addEventListener(Event.SELECT, onFileSelect);
}

private function onFileSelect(e:Event):void{
	//读文件
	var fs:FileStream = new FileStream();
	fs.open(File(e.target), FileMode.READ);
	var txt:String = fs.readUTFBytes(fs.bytesAvailable);
	fs.close();
	
	filePath = File(e.target).nativePath; //文件路径
	xmlList = new XMLList(txt); //文件内容
	
	//获取节点值
	lot1.text = xmlList.children()[0].lot;
	netValue1.text = xmlList.children()[0].net;
	
	lot2.text = xmlList.children()[1].lot;
	netValue2.text = xmlList.children()[1].net;
}

private function saveConfig():void{
	//设置节点值
	xmlList.children()[0].lot = lot1.text;
	xmlList.children()[0].net = netValue1.text;
	xmlList.children()[1].lot = lot2.text;
	xmlList.children()[1].net = netValue2.text;
	
	//写文件
	var fs:FileStream = new FileStream();
	fs.open(new File(filePath), FileMode.WRITE); 
	fs.writeUTFBytes(xmlList.toXMLString()); 
	fs.close(); 
}

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