CSV to XML 変換
csvファイル、もしくはcsv形式なtxtファイルをxmlファイルに変換します
楽しく使ってね 仲良く使ってね
Shift-JISが化けるんだけどどうすればいいんだろ
package
{
import com.bit101.components.PushButton;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import net.hires.debug.Stats;
import org.osmf.media.LoadableMediaElement;
[SWF(width="465", height="465", frameRate="60", backgroundColor="0xffffff")]
public class Main extends Sprite
{
private var lines:Array = [];
private var result:String = '';
private var tf:TextField;
private var btn1:PushButton;
private var btn2:PushButton;
private var csvFile:FileReference;
private var outFile:FileReference;
public function Main()
{
init();
}
private function init():void
{
tf = new TextField();
tf.autoSize = TextFieldAutoSize.LEFT;
var f:TextFormat = new TextFormat('_sans',24);
tf.defaultTextFormat = f;
addChild(tf);
tf.y = 10;
tf.text = '\ncsvをxmlに変換します\nファイルをload→saveです\n.csvファイルだと文字化けしたりするので\n一旦UTF-8な.txtファイルにして\nそれをloadするといいと思います\n';
btn1 = new PushButton();
btn1.label = 'load file';
addChild(btn1);
btn1.move(100,200);
btn1.addEventListener(MouseEvent.CLICK, onLoadClick);
btn2 = new PushButton();
btn2.label = 'save file';
addChild(btn2);
btn2.move(250,200);
btn2.addEventListener(MouseEvent.CLICK, onSaveClick);
btn2.visible = false;
csvFile = new FileReference();
outFile = new FileReference();
result = '';
}
private function onLoadClick(evt:MouseEvent):void
{
csvFile.addEventListener(Event.SELECT, onFileSelect);
csvFile.browse();
}
private function onFileSelect(evt:Event):void
{
csvFile.removeEventListener(Event.SELECT, onFileSelect);
csvFile.addEventListener(Event.COMPLETE, onFileLoadComp);
csvFile.load();
}
private function onFileLoadComp(evt:Event):void
{
this.result = '<?xml version="1.0" ?>\n<response>\n';
csvFile.removeEventListener(Event.COMPLETE, onFileLoadComp);
var string:String = String(csvFile.data);
lines = string.split(/\n|\n\r|\r\n|\r/);
for(var i:int = 0; i < lines.length; i ++)
{
this.result += ' <item>\n';
var s:String = lines[i];
var arry:Array = s.split(',');
for(var j:int = 0; j < arry.length; j++)
{
this.result += ' <param' + j + '>' + arry[j]+'</param' + j + '>\n';
}
this.result += ' </item>\n';
}
this.result += '</response>';
this.btn2.visible = true;
}
private function onSaveClick(evt:MouseEvent):void
{
var fileref:FileReference = new FileReference();
fileref.save(this.result, 'result.xml');
}
}
}