7 Replies Latest reply on Sep 26, 2014 10:57 AM by Dirk Becker

    add node to xml file

    bastieneichenberger Level 1

      Hi,

       

      I try to parse an xml file by the following way:

       

      // read the content
          var xml_file = File(xml_file_path);
          xml_file.open('r');
          var content = xml_file.read();
          xml_file.close();
      
          // add the node
          var root = new XML(content);
          var unit_test_resolution = <unit-test>{resolution}</unit-test>;
          root.appendChild(unit_test_resolution);
      
          // replace the file
          var my_file = new File(xml_file_path);
          var edit_content = root.toXMLString();
          my_file.open('w');
          my_file.writeln('<?xml version="1.0" encoding="UTF-8"?>');
          my_file.writeln(edit_content);
      

       

      I'm not sure that is a good solution can anyone help me? For example, my file already has the '<?xml version="1.0" encoding="UTF-8"?>' header. But if I read it by this way I have to put it each time egain …

       

      In advance thanks you for your help.

      Best regards.
      Bastien

        • 1. Re: add node to xml file
          xbytor2 Level 4

          This is from xtools/xlib/stdlib.js. I've been using it for several years to handle my xml file reading and writing.

           

          Stdlib.readXMLFile = function(fptr) {
            var rc;
            var file = Stdlib.convertFptr(fptr);
            if (!file.exists) {
              Error.runtimeError(48); // File/Folder does not exist
            }
            file.encoding = "UTF8";
            file.lineFeed = "unix";
            rc = file.open("r", "TEXT", "????");
            if (!rc && Stdlib.IOEXCEPTIONS_ENABLED) {
              Error.runtimeError(Stdlib.IO_ERROR_CODE, Stdlib.fileError(file));
            }
            var str = file.read();
            rc = file.close();
            if (!rc && Stdlib.IOEXCEPTIONS_ENABLED) {
              Error.runtimeError(Stdlib.IO_ERROR_CODE, Stdlib.fileError(file));
            }
            return new XML(str);
          };
          Stdlib.writeXMLFile = function(fptr, xml) {
            var rc;
            if (!(xml instanceof XML)) {
              Error.runtimeError(19, "xml"); // "Bad XML parameter";
            }
            var file = Stdlib.convertFptr(fptr);
            file.encoding = "UTF8";
            rc = file.open("w", "TEXT", "????");
            if (!rc && Stdlib.IOEXCEPTIONS_ENABLED) {
              Error.runtimeError(Stdlib.IO_ERROR_CODE, Stdlib.fileError(file));
            }
            // unicode signature, this is UTF16 but will convert to UTF8 "EF BB BF"
            // optional
            //file.write("\uFEFF");
            file.lineFeed = "unix";
            file.writeln('<?xml version="1.0" encoding="utf-8"?>');
            rc = file.write(xml.toXMLString());
            if (!rc && Stdlib.IOEXCEPTIONS_ENABLED) {
              Error.runtimeError(Stdlib.IO_ERROR_CODE, Stdlib.fileError(file));
            }
            rc = file.close();
            if (!rc && Stdlib.IOEXCEPTIONS_ENABLED) {
              Error.runtimeError(Stdlib.IO_ERROR_CODE, Stdlib.fileError(file));
            }
            return file;
          };
          

           

          Lots of paranoia coding in there, but after doing PSJS coding for a decade or so paranoia is a safer state of mind.

           

          -X

          • 2. Re: add node to xml file
            bastieneichenberger Level 1

            Hi, thanks for the reply … It's the same case here the author put the header again each time … I will use your example it's safer :) I didn't know your library lib/xlib ? Do you know where I can find it ? Thanks!

            • 3. Re: add node to xml file
              xbytor2 Level 4

              https://sourceforge.net/projects/ps-scripts/files/xtools/

               

              The current rev is 2_1.
              Lots of stuff in there.

               

              Enjoy.

               

              -X

              • 4. Re: add node to xml file
                bastieneichenberger Level 1

                Thanks a lot !!!

                • 5. Re: add node to xml file
                  Dirk Becker Level 4

                  Hi X,

                   

                  for completeness sake you can also watch the rc.error property ...

                  I think one test case where that helped me was malformed UTF8.

                   

                  Dirk

                  • 6. Re: Re: add node to xml file
                    xbytor2 Level 4

                    rc is a simple boolean return code from file function calls. If it's true, all is well. If it's false, there was a problem with details in file.error. The call Stdlib.fileError(file) returns a human readable error message of the file error. I think the code above handles malformed UTF8. If it doesn't, this chunk of code should. I spent a couple of days sorting out the character conversion problem. I should probably change readXMLFile to call this instead of just reading the file raw.

                     

                    
                    
                    Stdlib.readFromFile = function(fptr, encoding, lineFeed) {
                      var file = Stdlib.convertFptr(fptr);
                      var rc;
                      rc = file.open("r");
                      if (!rc) {
                        Error.runtimeError(Stdlib.IO_ERROR_CODE,
                                           Stdlib.fileError(file, "Unable to open input file "));
                      }
                      if (encoding) {
                        file.encoding = encoding;
                      }
                      if (lineFeed) {
                        file.lineFeed = lineFeed;
                      }
                      var str = file.read();
                      // in some situations, read() will set the file.error to
                      // 'Character conversion error' but read the file anyway
                      // in other situations it won't read anything at all from the file
                      // we ignore the error if we were able to read the file anyway
                      if (str.length == 0 && file.length != 0) {
                        if (!file.error) {
                          file.error = 'Probable Character conversion error';
                        }
                        if (Stdlib.IOEXCEPTIONS_ENABLED) {
                          Error.runtimeError(Stdlib.IO_ERROR_CODE, Stdlib.fileError(file));
                        }
                      }
                      rc = file.close();
                      if (!rc && Stdlib.IOEXCEPTIONS_ENABLED) {
                        Error.runtimeError(Stdlib.IO_ERROR_CODE, Stdlib.fileError(file));
                      }
                      return str;
                    };
                    

                     

                    The related Stdlib.writeToFile function is fairly simple except for the fact that PS7 did not support BINARY encoding correctly. That took some gymnastics and a couple of beers to get it working correctly.

                     

                    Also note that I use Error.runtimeError instead of throw. The reason for this is that you get a better error stack trace for logging errors. See Stdlib.exceptionMessage for a way in which this this is taken to good advantage. I got the idea from Bob Stucky and tweaked it to a state that I've used for years now.

                    • 7. Re: Re: add node to xml file
                      Dirk Becker Level 4

                      Sorry, I meant file.error of course, after file.read().

                      As you say, and after a closer look at your enormous library I see that's handled in Stdlib.fileError most of the time.

                       

                      Dirk