8 Replies Latest reply on Nov 20, 2010 10:19 AM by Claudiu Ursica

    Matching entry already in a xml file

    mroiler

      I need to make a script like this:

       

      if /*the fields name and email already exist in the xml file*/

      {

      Alert.show("Sorry you already used this form");

      }

      else

      {

           var file:File = File.desktopDirectory;
                      file = file.resolvePath("data.xml");
                      var myXML:String =
                          <Person>
                              <Name>{name.text}</Name>
                              <Email>{email.text}</Email>
                          </Person>;
                      var fs:FileStream = new FileStream();
                      fs.open(file, FileMode.APPEND);
                      fs.writeUTFBytes(myXML);
                      fs.close();
                      Alert.show("Thank You!");

      }

       

      what would the comment by if be?

        • 1. Re: Matching entry already in a xml file
          Claudiu Ursica Level 4

          I am assuming your saved XML will look like something like this:

           

                               

           

          If you maintain the whole structure in memory you just need to perform a lookup

          for the name, with the assumption that the name is unique. If you dump it all

          the time on the disk as the code tends to imply you need to read it all the time

          perform the lookup and alert if found.

           

           

          var file:File = File.documentsDirectory.resolvePath("something.xml"); //use here

          whatever path algorithm you use for saving

          var fileStream:FileStream = new FileStream();

          fileStream.open(file, FileMode.READ);

          var inXML:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));

          fileStream.close();

          for each (var xml:XML in Person)

          {

              if (xml.Name == name.text)

                {

                  //Already there.

               }

          }

           

           

          You have a rough idea now. If names ar enot unique add an extra id to the xml

          and perform the lookup based on ID.

           

          HTH,

          Claudiu

           

          P.S. the code is just me typing so it might have errors.

          1 person found this helpful
          • 2. Re: Matching entry already in a xml file
            mroiler Level 1

            Thats exactly what i need but i can't get past this error:

             

            1120: Access of undefined property Person.

             

            It's on the line with "for each"

            • 3. Re: Matching entry already in a xml file
              Claudiu Ursica Level 4

              That is because I typed in stupid stuff :).

               

              Hope this XML appears in the end cause I'm typing from my email editor...

              Sample XML - need to have a root tag to your XML when you dump it on the disk...

               

              <root>
              <Person>
                  <Name>Name</Name>
                  <Email>Email</Email>
              </Person>
              <Person>
                  <Name>Name</Name>
                  <Email>Email</Email>
              </Person>
              <Person>
                  <Name>Name</Name>
                  <Email>Email</Email>
              </Person>
              <Person>
                  <Name>Name</Name>
                   <Email>Email</Email>
              </Person>
              </root>

               

               

              var inXML:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));

               

              for each (var xml:XML in inXML.Person)

              {

                  if (xml.Name == name.text)

                    {

                      //Already there.

                   }

              }

               

              This should run.

               

              inXml hold the whole xml, inXML.Person will return an XMLList with the person nodes.

               

              C

              1 person found this helpful
              • 4. Re: Matching entry already in a xml file
                mroiler Level 1

                it worked but where i have it save it if it doesn't exsit, it puts it at the end of the file because the filemode is APPEND. how do i make it go in the root node?

                • 5. Re: Matching entry already in a xml file
                  mroiler Level 1

                  never mind i figured that out but this is what my xml file has weird boxes on where a properly formated xml file would be a new line but not on the newest entry. I need this for excel so i can't have those weird boxes. this is my new code

                   

                   

                   

                  var file:File = File.desktopDirectory.resolvePath("data.xml");

                  var fileStream:FileStream = new FileStream();

                  fileStream.open(file, FileMode.READ);

                  var inxml:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));

                  fileStream.close();

                  for each (var xml:XML in inxml.Person)

                  {

                  if (xml.Fname == nameFirst.text && xml.Lname == nameLast.text)

                  {

                  Alert.show("You have already used this form");

                  nameFirst.text == null;

                  nameLast.text == null;

                  }

                  else

                  {

                  var myXML:String = "<Person><Fname>" + nameFirst.text + "</Fname><Lname>" + nameLast.text + "</Lname></Person>";

                  var all:String = "<?xml version='1.0' encoding='utf-8'?>" + "<root>" + inxml.Person + myXML + "</root>";

                  var fs:FileStream = new FileStream();

                  fs.open(file, FileMode.WRITE);

                  fs.writeUTFBytes(all);

                  fs.close();

                  Alert.show("Thank You! You have now been entered.","Thank You!");

                  nameFirst.text == null;

                  nameLast.text == null;

                   

                  }

                   

                   

                   

                  I also tried converting inxml to a string before adding it with everything else. it didn't do anything.

                  • 6. Re: Matching entry already in a xml file
                    Claudiu Ursica Level 4

                    Not sure I understand, can you attach a screenshot? As an alternative if XML

                    doesn;t cut it you can use SQLite  to dump your data.

                     

                    C

                    • 7. Re: Matching entry already in a xml file
                      mroiler Level 1

                      cant do anything like that it has to all be in the app nothing else even the internet.At the <root> tag and second <Person> tag I hit enter after so you could see it all because before it was all on one line.

                      untitled.JPG

                      • 8. Re: Matching entry already in a xml file
                        Claudiu Ursica Level 4

                        Air databases are contained within the app. SO you can still dump data in the

                        database.

                        when you drop your xml and create it from string...

                         

                        + "";

                         

                         

                        that is an XMLList try to add an toString() e.g  inxml.Person.toString() to make

                        sure u concatenate stings. If is not working share the whole file, when I have

                        some time I'll take a look. I generated a small XML and dumped it and it worked

                        fine for me ...

                         

                        C