Skip to main content
Participating Frequently
November 24, 2010
Answered

Parse text doc into xml ?

  • November 24, 2010
  • 2 replies
  • 1878 views

Hi All

I have an xml file like this

<Controls>
    <Entry>
      <Key></Key>
      <Down></Down>
    </Entry>
    <Entry>
      <Key></Key>
      <Down></Down>
    </Entry>
</Controls>

And a text document like this

Throttle_10,F2+1
Throttle_20,F2+2
Throttle_30,F2+3
Throttle_40,F2+4
Throttle_50,F2+5
Throttle_60,F2+6
Throttle_70,F2+7
Throttle_80,F2+8
Throttle_90,F2+9

What i need to be able to do is to add the first element of the text document to the <key></key> and the second element to <down></down> of the xml file ...

How would i best go about this please ?

Many Thanks 

This topic has been closed for replies.
Correct answer Andrei1-bKoviI

Thats excellent, many thanks for your help !!!

All i have to do is save this to an xml file now ...

Why does the addEventListener have to be added before the load() method gets called ?

Many Thanks


You are welcome. Please mark the thread as answered.

When you call load before adding listeners, especially in the local environment, things happen instantly and file is actually loaded before event is dispatched so your application may not hear this event. In other words, when you call load() things start happening right away but event listeners are added after load starts executing.

2 replies

Inspiring
November 25, 2010

Assuming your text file is generated with line breaks here is the code that composes XML out of this text file (see output below):

var loadedText:String = "Throttle_10,F2+1\nThrottle_20,F2+2\nThrottle_30,F2+3\nThrottle_40,F2+4\nThrottle_50,F2+5\nThrottle_60,F2+6\nThrottle_70,F2+7\nThrottle_80,F2+8\nThrottle_90,F2+9";
var loadedArray:Array = loadedText.match(/Throttle.*(?=[\n\r]*)/g);
var xml:XML = <Controls/>;
var entry:XML;
var key:XML;
var down:XML;
for each(var value:String in loadedArray) {
     entry = <Entry/>;
     key = <Key/>;
     down = <Down/>;
     key.appendChild(value.match(/.*(?=,)/));
     down.appendChild(value.match(/(?<=,).*/));
     entry.appendChild(key);
     entry.appendChild(down);
     xml.appendChild(entry);
}

trace(xml);

Trace output:

<Controls>
  <Entry>
    <Key>Throttle_10</Key>
    <Down>F2+1</Down>
  </Entry>
  <Entry>
    <Key>Throttle_20</Key>
    <Down>F2+2</Down>
  </Entry>
  <Entry>
    <Key>Throttle_30</Key>
    <Down>F2+3</Down>
  </Entry>
  <Entry>
    <Key>Throttle_40</Key>
    <Down>F2+4</Down>
  </Entry>
  <Entry>
    <Key>Throttle_50</Key>
    <Down>F2+5</Down>
  </Entry>
  <Entry>
    <Key>Throttle_60</Key>
    <Down>F2+6</Down>
  </Entry>
  <Entry>
    <Key>Throttle_70</Key>
    <Down>F2+7</Down>
  </Entry>
  <Entry>
    <Key>Throttle_80</Key>
    <Down>F2+8</Down>
  </Entry>
  <Entry>
    <Key>Throttle_90</Key>
    <Down>F2+9</Down>
  </Entry>
</Controls>

Participating Frequently
November 25, 2010

This is what i have so far

        private function loadtext(event:Event):void         {             var loadText:URLLoader = new URLLoader();             loadText.load( new URLRequest('Data/FSXKeyboardFormatted.txt'));             loadText.addEventListener(Event.COMPLETE, textLoaded);         }         private function textLoaded(event:Event):void         {             var loadedText:URLLoader = URLLoader(event.target);             var myData:String = new String(event.target.data);             var arrayData:Array = myData.split(',');             for( var i = 0; i < arrayData.length; i++ )             {             }             trace(arrayData);         }

But i am not sure now how to pass this to the xml document ....

Many Thanks

Inspiring
November 25, 2010

Here is how to plug in my code into your flow:

private function textLoaded(event:Event):void
{
     var loadedText:URLLoader = URLLoader(event.target);
     var myData:String = new String(event.target.data);
     makeXML(event.target.data);
}

private function makeXML(data:String) {
     var loadedArray:Array = data.match(/Throttle.*(?=[\n\r]*)/g);
     var xml:XML = <Controls/>;
     var entry:XML;
     var key:XML;
     var down:XML;
     for each(var value:String in loadedArray) {
          entry = <Entry/>;
          key = <Key/>;
          down = <Down/>;
          key.appendChild(value.match(/.*(?=,)/));
          down.appendChild(value.match(/(?<=,).*/));
          entry.appendChild(key);
          entry.appendChild(down);
          xml.appendChild(entry);
     }
     
     trace(xml);
}

Fl_as_he_ro
Participant
November 24, 2010

Hey wacth this!

1. Step

http://forums.adobe.com/thread/756738?tstart=0