2 Replies Latest reply: Dec 12, 2011 10:27 PM by 904710 RSS

    Write XML file as source file after value updating

    904710
      Thanks for reading this :
      I have XML file which is more than 15 mb . I want to update some of it tag's value not tags. After updating the values when i saved file there are some additional changes by the java which I am not required , becoz this file is third party application . Plz resolve this.

      e.g. base file
      1. <?xml version="1.0"?>
      2. <sp></sp>
      3. <ch></ch>
      4. <r></r>
      5. <val></val>

      Updated file showing as :
      1. <?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE ggFile SYSTEM "yes">
      2. <sp/>
      3. <ch/>
      4. <r/>
      5. <val/>


      Saving method using by me this
      public void saveXMLAfterUpdating(File file, Document doc) throws Exception {

      TransformerFactory factory1 = TransformerFactory.newInstance();
      Transformer transformer = factory1.newTransformer();
      transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "yes");
      StringWriter writer = new StringWriter();
      StreamResult result = new StreamResult(writer);
      DOMSource source = new DOMSource(doc);
      transformer.transform(source, result);
      String s = writer.toString();

      FileWriter fileWriter = new FileWriter(file);
      BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
      bufferedWriter.write(s);
      bufferedWriter.flush();
      bufferedWriter.close();

      }

      I required this on urgent basis. Plz reply ASAP..
        • 1. Re: Write XML file as source file after value updating
          EJP
          I want to update some of it tag's value not tags.
          XSLT or XPath.
          After updating the values when i saved file there are some additional changes by the java which I am not required , becoz this file is third party application.
          So is that relevant to your question?
          Plz resolve this.
          Only you can resolve the part about 'additional changes'.
          e.g. base file
          1. <?xml version="1.0"?>
          2. <sp></sp>
          3. <ch></ch>
          4. <r></r>
          5. <val></val>

          Updated file showing as :
          1. <?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE ggFile SYSTEM "yes">
          That's just a matter of setting a DOCTYPE, not a problem.
          2. <sp/>
          3. <ch/>
          4. <r/>
          5. <val/>
          None of these transformations is necessary, as they are entirely equivalent to the original. However you are likely to get them free out of any proper XML API or tool anyway, there is no effort required.

          There are no actual updates to any tag values in the above. Did you have further unstated requirements or have you just mis-stated them?

          It should also be noted that neither the original nor the result is a valid XML file unless there is more to them than you have shown. It would surprise me if you got any output via that code with that data.
          StringWriter writer = new StringWriter();
          Why waste time and space with a StringWriter? Just use a FileWriter here and transform directly to the file ...
          FileWriter fileWriter = new FileWriter(file);
          BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
          bufferedWriter.write(s);
          bufferedWriter.flush();
          bufferedWriter.close();
          ... and delete all this.
          I required this on urgent basis.
          Your scheduling problems are your own. This is a user to user forum and nobody cares about your schedule except you. Putting statements like that tends only to antagonize people, i.e. has the reverse of the desired effect.
          • 2. Re: Write XML file as source file after value updating
            904710
            Sorry If i made any wrong query . Thank You very much for your reply . Let me check the things ....