javaxmlfilejoox

save modifications in xml file with joox


i have xml file and i want to add elements and save it with joox

my xml file

    <document>
  <library name="Amazon">
    <books>
      <book id="1">
        <name>1984</name>
        <authors>
          <author>George Orwell</author>
        </authors>
      </book>
      <book id="2">
        <name>Animal Farm</name>
        <authors>
          <author>George Orwell</author>
        </authors>
      </book>
    </books>
  </library>
</document>

my java code

        File xmlfile = new File("C:\\SiteMap\\Doc.xml");

        Document document = $(xmlfile).document();

Match x = $(document).xpath("//book[@id = 1]");
        System.out.println(""+x.toString());

       //this my modification 
        x.append("<loc/>");       

but now i want to save this modification my Doc.xml file


Solution

  • Simply use $(document).write(xmlfile); to overwrite the file.