1 Reply Latest reply on May 18, 2014 1:12 AM by Damienned

    split txt-file into 3 xml files

    Damienned Level 1

      I'm trying to create 3 xml files from 1 txt file in javascript.

       

      What i have sofar:

       

      A text file containing:

       

      GP0006896###SunGold-3 small###00###Zespri Service Centre NV###KLT0000110###16950###tray Kiwi SunGold 3-pack Small###JWM###ZESPRI-3-D$$$

      GP0012816###S500-R012336###00###Poppies Nederland BV###KLT0000081###17890###Plus slagroomsoes 250g###JWM###CULI-10-B$$$

      GP0009000###test###01###Just a test###KLT0001010###20596###Dit is een test regel###Standaard###TEST-01-B$$$

       

      The script:

       

      // Set variable

      var txtfolderpath = app.path+'/XML_converter/';

      var txtfilename = 'txt_to_xml.txt';

      var txtfilelocation = File(txtfolderpath+txtfilename);

      var xmlfolderpath = app.path+'/XML_converter/xml/';

      var retChar = eval('String.fromCharCode(13)');

      var presscancelbutton = 0;

      var appversion = app.version;

      var appversionarray = appversion.split('.');

      appversion = new Number(appversionarray[0]);

      var xmlstring='';

      //Scriptfolderpathcheck

      #target Illustrator

      var pathcheck = File($.fileName).parent.fsName;

      infopanelmenu();

      function infopanelmenu(){

          res =

          "dialog { \

              text:'TXT to XML:', \

              properties:{resizeable:false}, \

              margins: [0,0,0,0], \

                  imagegroup: Group { orientation:'stack', preferredSize: [400, 221], alignChildren:'bottom', \

                      jobgroup: Group { orientation:'column',  margins: [0,0,0,20], \

                          infopanelgroup: Panel { orientation:'column', preferredSize: [360, 20], alignChildren:'top', text: 'TXT to XML conversion', \

                              xmlgroup: Group { orientation: 'row', alignment: 'left', \

                                  s: StaticText { text:'XML converted : ' }, \

                                  s2: StaticText { text:'0' }, \

                              } \

                              statusgroup: Group { orientation: 'row', alignment: 'left', \

                                  s: StaticText { text:'Status              : ' }, \

                                  s2: StaticText { text:'Ready to start' }, \

                              } \

                          } \

                          infopanelbuttons: Group { orientation: 'row', alignment: 'center', \

                          cancelBtn: Button { text:'Cancel', properties:{name:'cancel'} }, \

                          applyBtn: Button { text:'Start', properties:{name:'apply'} }, \

                      } \

                  } \

              } \

          }";

          wininfo = new Window (res);

          wininfo.frameLocation = [100, 100];

          // Buttons

          wininfo.imagegroup.jobgroup.infopanelbuttons.cancelBtn.onClick = function () {

              presscancelbutton = presscancelbutton - 1;

              wininfo.close();

          };

          wininfo.imagegroup.jobgroup.infopanelbuttons.applyBtn.onClick = function () {

              buildxml();

          };

          wininfo.center();

          wininfo.show();

      }

      function buildxml(){

          txtfilelocation.open('read');

          txtfile = txtfilelocation.read();

          var xmlarray = txtfile.split('$$$');

          wininfo.imagegroup.jobgroup.infopanelgroup.statusgroup.s2.text = 'Running';

          wininfo.imagegroup.jobgroup.infopanelbuttons.applyBtn.text = 'Running';

          wininfo.imagegroup.jobgroup.infopanelbuttons.applyBtn.enabled = false;

          // For lus schrijven

          for (i=0; i<xmlarray.length-1; i++) {

              var subxmlarray = xmlarray[i].split('###');

              var counter = i+1;

              var xmlstring = '';

              xmlstring = xmlstring + '<?xml version="1.0" encoding="UTF-8"?>'+retChar;

              xmlstring = xmlstring + '<xml>'+retChar;

              xmlstring = xmlstring + '<gp_number>'+subxmlarray[0]+'</gp_number>'+retChar;

              xmlstring = xmlstring + '<artikelnummer>'+subxmlarray[1]+'</artikelnummer>'+retChar;

              xmlstring = xmlstring + '<versienummer>'+subxmlarray[2]+'</versienummer>'+retChar;

              xmlstring = xmlstring + '<klantnaam>'+subxmlarray[3]+'</klantnaam>'+retChar;

              xmlstring = xmlstring + '<klantnummer>'+subxmlarray[4]+'</klantnummer>'+retChar;

              xmlstring = xmlstring + '<ID>'+subxmlarray[5]+'</ID>'+retChar;

              xmlstring = xmlstring + '<artikelomschrijving>'+subxmlarray[6]+'</artikelomschrijving>'+retChar;

              xmlstring = xmlstring + '<workflow>'+subxmlarray[7]+'</workflow>'+retChar;

              xmlstring = xmlstring + '<stans>'+subxmlarray[8]+'</stans>'+retChar;

              xmlstring = xmlstring + '</xml>'

              wininfo.imagegroup.jobgroup.infopanelgroup.xmlgroup.s2.text = counter;

              alert(xmlstring); // see if the stingbuild works <-- works here 3 times

              // Save function

              var filename = subxmlarray[0]+'.xml';

              var xmlsavelocation = File(xmlfolderpath+filename);

              xmlsavelocation.open('w');

              xmlsavelocation.writeln(xmlstring);// save the string <-- works here 3 times but generates two empty documents!

              xmlsavelocation.close();

          }

          txtfilelocation.close();

          wininfo.imagegroup.jobgroup.infopanelgroup.statusgroup.s2.text = 'Ready';

          wininfo.imagegroup.jobgroup.infopanelbuttons.applyBtn.text = 'Start';

          wininfo.imagegroup.jobgroup.infopanelbuttons.applyBtn.enabled = true;

      }

       

      The output:

       

      output.png

       

      You'll see that i get 3 files but 2 are 0 bytes.

       

      The question:

       

      Why can't i save the other two strings in a xml file?

       

      Thanks Daniel