12 Replies Latest reply on Apr 29, 2009 9:48 PM by Threadstone

    Parsing a Text file

    Threadstone Level 1

      Hello All,

       

      I want to parse a text file and I would like to display data in text fields , combo boxes etc..

       

      So, here is my code.

       

      I have the text object.

       

      <?xml version="1.0" encoding="utf-8"?>

      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

         creationComplete="dataRequest.send()">

        <mx:Script>

          <![CDATA[

            import mx.rpc.events.ResultEvent;

            import mx.controls.Alert;

            [Bindable]

            private var dataObj:Object = new Object();

           // public var s1:String = dataObj.toString();

            public var s3:String = "PONumber";

            public var s2:String;

            public var po:int;

            private function resultHandler(e:ResultEvent):void {

            dataObj = e.result;

            }

            public function Parse():void

           {

             po = dataObj.toString().search(s3);

             s2 =  s3.substring(po+8,15);

            }

          ]]>

        </mx:Script> 

        <mx:HTTPService id="dataRequest" useProxy="false" resultFormat="text"

          result="resultHandler(event)" url="http://Localhost/po.txt"/> 

        <mx:TextArea id="txt" text="{dataObj.toString()}" width="578" height="179"/>

        <mx:Button id="myButton" label="Copy Text" click= "Alert.show(s2, 'Alert Box', mx.controls.Alert.OK);"/> 

      </mx:Application>

       

      I have something like this in the text file.

       

      PONumber 4500213514-3408

       

      I need - 4500213514-3408 from that., the alert bopx is showing blank.

       

      How can I extract that number from the text file.Where I went wrong,

       

      The "PONumber" will be my key string.

       

      Many thanks,

        • 1. Re: Parsing a Text file
          ATIF FAROOQ Level 3

          hi,

             you can use such regular Expression

               <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="onComplete(event)">
               <mx:Script>
                    <![CDATA[
          
                          public var regexp:RegExp=/\d{10}-\d{4}/;
                          public function onComplete(e:Event):void{
                              trace(regexp.exec("PONumber 4500213514-3408")) 
                          }         
                      ]]>
           </mx:Script>
          
          </mx:Application>
          
          • 2. Re: Parsing a Text file
            m_hartnett Level 3

            You can use the subStr method.  Different from the subString

             

            var po:String = "987777555-997"

             

             

             

             

             

            var lastBit:String = po.substr(9,po.length - 9)

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

             

            means start from the 9th position PONumber plus a space and get the length of the string minus 9 characters.

             

            Couple of other ways to do this as well but if you always know that PONumber is the key this will work fine

            • 3. Re: Parsing a Text file
              Barna Biro Level 3

              Hi there,

               

              Using regular expressions would be the best solution because regular expressions are faster ( more performant ) than working with the methods the String or StringUtils class can offer.

               

              With best regards,

              Barna Biro

              • 4. Re: Parsing a Text file
                m_hartnett Level 3

                While regular expressions are faster you need to take the situation into consideration.

                If there is a lot of data that you are searching through then the regEx is a better way of searching for the data.

                 

                However if it is a simple extract of data from a know string of data where you have a defined starting point then the subStr or subString methods are fine.

                 

                The time between a regEx and a subStr in this situation would be unnoticable.

                • 5. Re: Parsing a Text file
                  Barna Biro Level 3

                  If we develop our producs to be as efficient/performant as possible then even these minor decisions could count on the long run. I try to always "plan for the future", he may not need the power of regExp today, but maybe tomorrow, he'll want to work with tons of data instead of a smaller piece. He would be "forced" to go back and replace substring with a regular expression just to get that tiny performance boost that could count a lot later on.

                   

                  I'm not saying that using substring is wrong, just that why ignore the solution that not only does the same thing as substring but it also offers more advantages on the long run ( making your code more "open to change" )? I think it's up to everyone's choice, I prefer to plan for the future and try to get my stuff working as well as possible from the start than have to waste hours/days refactoring tons and tons of code later on.

                   

                  Best regards,

                  Barna Biro

                  • 6. Re: Parsing a Text file
                    Michael Borbor Level 4

                    Just an idea but wouldn't be simpler if you just use CVS files?

                    • 7. Re: Parsing a Text file
                      Barna Biro Level 3

                      Yeah, CVS files would definitely get rid of this parsing issue but maybe he is not allowed to use CVS files. Nonetheless, a good idea.

                       

                      Kind regards,

                      Barna Biro

                      • 8. Re: Parsing a Text file
                        Threadstone Level 1

                        Hi Friends,

                         

                        Here in this scenario a pdf file would be converted to a text file.The data(values against specific identifier) in the text file is not fixed.

                         

                        Here the string "PONumber" is fixed which refers to a number(with "-") as I mentioned earlier.

                         

                        I can say, the format the text file will be the same always.

                         

                        I need to search for the string "PONumber" in the text file and then I need to fetch the value "4500213514-3408" (which would be changing always).

                         

                        I don't have any choice like csv, excel etc..

                         

                        Please suggest the required code changes !

                         

                        Thanks.

                        • 9. Re: Parsing a Text file
                          Barna Biro Level 3

                          By text file do you mean .txt file? If so, then it would be a real pain to search trough a .txt file and try to retrieve data somehow. Instead, when you parse the PDF file you should add all the data in a incely structured XML file. With the help of E4X you'll be able to search trough the nodes of the XML file quite easily and retrieve whatever informations you need. Things aren't that simple with a plain text file, you'd essentially need to slice the whole text up so you can compare each individual piece to the "pattern" you are looking for ( this would be a really performance costy operation and should not be used ).

                           

                          I think that parsing the PDF to a nicelt structured XML file is your best solution. If not, then you are kind left with the "looping trough the whole text after it's sliced into pieces and compare each piece to the piece you are looking for".

                           

                          With best regards,

                          Barna Biro

                          • 10. Re: Parsing a Text file
                            m_hartnett Level 3

                            If you would like to search through the text then you can do the following.

                            You need to remember that searching through data is dependent on rules.

                            For example:

                                 Does PONumber only appear once in the text or are there multiple occurance of that string.

                                 Will the po number always be 15 characters

                                 Will the po number always hava a dash (-) in it.

                                 Will the po number always have 4 characters after the dash.

                               

                            If you can come up with a set of rules for searching through the data then these will work.  If the data is going to be very variable then another approach would be necessary.

                                

                             

                            <?xml version="1.0" encoding="utf-8"?>

                            <mx:Application

                                 xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical"

                                 width="100%" height="100%" backgroundColor="#eaf1f6"

                                 backgroundGradientAlphas="1.0,1.0" creationComplete="cc()">

                             

                                 <mx:Script>

                                      <![CDATA[

                                           import mx.controls.Button;

                                           import mx.events.IndexChangedEvent;

                                           import mx.collections.ArrayCollection;

                             

                             

                                           private function cc() : void {

                                                poNbrTxt.text= "Here is some random text data in a pdf file " +

                                                                         "that has been converted to text. I am searching an " +

                                                                         "identifier and another number after " +

                                                                         "the the text such as PONumber 4500213514-3408. When it is " +

                                                                         "found then it will be extracted"

                                           }

                             

                                           public function findTextData():void {

                             

                             

                                                var pos:int = poNbrTxt.text.indexOf("PONumber") + 9;

                             

                                               

                                                //if the ponumber is always 15 characters in length

                                                var pNbr:String = poNbrTxt.text.substr(pos,15)

                                

                             

                                                //if the number always contains an - then find where the dash is

                                                //starting from the position of the PONumber

                                                var pos1:int = poNbrTxt.text.indexOf("-",pos)

                             

                             

                                                //Get the po number based on the first pos and the end position adding

                                                //5 to this accounting for the dash and the additional four numbers.

                                                var pNbr1:String = poNbrTxt.text.substring(pos, pos1+5)

                                                foundText.text = "";

                                                foundText.text = pNbr + " - " + pNbr1

                                           }

                             

                             

                                 ]]>

                                 </mx:Script>

                                 <mx:TextArea height="133" width="234" id="poNbrTxt"/>

                                 <mx:TextArea height="133" width="234" id="foundText"/>

                                 <mx:Button click="findTextData()" label="Find it"/>

                            </mx:Application>

                            1 person found this helpful
                            • 11. Re: Parsing a Text file
                              Gregory Lafrance Level 6

                              This code should solve your problem:

                               

                              <?xml version="1.0" encoding="utf-8"?>
                              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                                creationComplete="singleDataRequest.send();multipleDataRequest.send();">
                                <mx:Script>
                                  <![CDATA[
                                    import mx.collections.ArrayCollection;
                                    import mx.rpc.events.ResultEvent;
                                    import mx.controls.Alert;
                                 
                                    [Bindable] private var dataString:String;
                                    [Bindable] private var dataAC:ArrayCollection = new ArrayCollection();

                                    private function resultHandler(e:ResultEvent):void {
                                      var str:String = e.result as String;
                                      Parse(e.currentTarget.url, str);
                                    }

                                    public function Parse(url:String, text:String):void{
                                      var pattern:RegExp = /PONumber\D+(\d+-\d+)/g;
                                      var arr:Array = text.match(pattern);
                                      if(url == "OnePO.txt"){
                                        dataString = arr[0].replace(/PONumber\D+/, "");
                                      }else{
                                        for each(var str:String in arr){
                                          dataAC.addItem(str.replace(/PONumber\D+/, ""));                     
                                        }
                                      }
                                    }
                                  ]]>
                                </mx:Script> 
                                <mx:HTTPService id="singleDataRequest" useProxy="false" resultFormat="text"
                                  result="resultHandler(event)" url="OnePO.txt"/> 
                                <mx:HTTPService id="multipleDataRequest" useProxy="false" resultFormat="text"
                                  result="resultHandler(event)" url="MultiplePO.txt"/> 
                                <mx:TextArea id="txt" text="{dataString}" width="578" height="179"/>
                                <mx:ComboBox id="cbx" dataProvider="{dataAC}"/>
                              </mx:Application>

                               

                              ------------------ OnePO.txt ------------------------

                              This is text in which I am embedding a single purchase order.

                               

                               

                              Here is the purchase order PONumber 4500213514-1001 within this line.

                               

                               

                              Another file in this example has multiple purchase order numbers.

                               

                              -------------- MultiplePO.txt -----------------------

                              This is text in which I am embedding purchase order information.

                               

                               

                              Here is one example: PONumber 4500213514-1001

                               

                               

                              PONumber 4500213514-2002

                               

                               

                              PONumber 4500213514-3003 The example to the left is at the start of a string.

                               

                               

                              Here are two on one line:

                               

                               

                              PONumber 4500213514-4004 and now this PONumber 4500213514-5005

                               

                               

                              And two between text PONumber 4500213514-6006 PONumber 4500213514-7007.

                               

                              Another PONumber 4500213514-9009 and another PONumber 4500213514-10010.

                               

                              1 person found this helpful
                              • 12. Re: Parsing a Text file
                                Threadstone Level 1

                                Thank you all , Your Inputs Made it

                                 

                                 

                                Thanks & Regards,