2 Replies Latest reply on Dec 17, 2008 2:16 PM by enorton@adobe

    Updating XML

    kiwi_ziesch Level 1
      Hi,
      I´ve created a user administration for my app and I want to allow the user to activate or deactivate the administration.
      When the user click´s "activate" I only want to change ony single attribute in the file and save this.
      But I don´t know how to do this.
      For now I´m reading the whole xml and save its data and then write the data with the new attribute (active). But I can´t write the data because it says "file in use". Sure, I opened 2 streams - to read and write. But I close the reading-one before I open the writing-one...Any ideas?
      Here´s the code:

      var userFile = air.File.applicationStorageDirectory;
      userFile = userFile.resolvePath("user.xml");
      var stream = new air.FileStream();

      if (userFile.exists) {

      stream.open(userFile, air.FileMode.READ);
      var userXML = stream.readUTFBytes(stream.bytesAvailable);

      var domParser = new DOMParser();
      userXML = domParser.parseFromString(userXML, "text/xml");

      var vorlage = userXML.documentElement.getElementsByTagName( 'user' );

      //user1
      var u_name1 = vorlage[0].getElementsByTagName('name')[0].textContent;
      var u_pass1 = vorlage[0].getElementsByTagName('passwort')[0].textContent;
      //user2
      var u_name2 = vorlage[1].getElementsByTagName('name')[0].textContent;
      var u_pass2 = vorlage[1].getElementsByTagName('passwort')[0].textContent;
      //user3
      var u_name3 = vorlage[2].getElementsByTagName('name')[0].textContent;
      var u_pass3 = vorlage[2].getElementsByTagName('passwort')[0].textContent;
      }
      stream.close();

      //write new data
      var cr = air.File.lineEnding;
      var user_data = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + cr
      + "<management>" + cr
      + " <user id=\"1\">" + cr
      + " <name>"+u_name1+"</name>" +cr
      + " <passwort>"+u_pass1+"</passwort>" + cr
      + " </user>" + cr
      + " <user id=\"2\">" + cr
      + " <name>"+u_name2+"</name>" + cr
      + " <passwort>"+u_pass2+"</passwort>" +cr
      + " </user>" +cr
      + " <user id=\"3\">" +cr
      + " <name>"+u_name3+"</name>" + cr
      + " <passwort>"+u_pass3+"</passwort>" +cr
      + " </user>" + cr
      + " <aktiv>true</aktiv>" + cr
      + "</management>" +cr

      var stream_write = new air.FileStream();
      stream_write.open(userFile, air.FileMode.WRITE);
      stream_write.writeUTFBytes(user_data, air.File.systemCharset);
      stream_write.close();

      I´m a bit confused about this.
      Thanks in advance.