How to Update an XML File in Java

104 29
    • 1). Create a file named "test.xml" in a text editor and save it to the C: drive. Paste the following sample XML database into it:

      <?xml version="1.1" encoding="ISO-8859-1"?>
      <USERS>
      <USER>
      <name>Alice Smith</name>
      <type>guest</type>
      </USER>
      <USER>
      <name>Bill Jameson</name>
      <type>guest</type>
      </USER>
      </USERS>

      As you can see, this is a user database that holds two users, Alice Smith and Bill Jameson, who hold guest accounts.

      However, some changes need to be made: Alice has gotten married and adopted her husband's last name, "Wilson."

      This sample database will be used for the rest of the tutorial.

    • 2). Create a new Java class and name it "XMLParser.java." You can do this in any text editor or with a dedicated Java Integrated Development Environment (IDE).

      Paste the following code within it:

      import com.sun.org.apache.xml.internal.serialize.OutputFormat;
      import com.sun.org.apache.xml.internal.serialize.XML11Serializer;
      import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
      import java.io.File;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import org.w3c.dom.Document;
      import org.w3c.dom.Element;
      import org.w3c.dom.Node;
      import org.w3c.dom.NodeList;
      import org.xml.sax.SAXException;

      public class XMLParser {

      public static void main(String[] args) {
      try {

      }

      }
      }

      All other code will go inside the try-block of the main method.

    • 3). Add the following line of Java code to open the XML file from the hard disk:

      Document d = xmlDocument.parse(new File("C:\test.xml"));

    • 4). Query the document based on a tag name. In this case, the best way to search will be to look for Alice Smith by name:

      // Build a list of all the names.
      NodeList nl = d.getElementsByTagName("name");

      // Search for the user named Alice Smith
      for (int x = 0; x< nl.getLength(); x++) {
      Node nameNode = nl.item(x);

      if (nameNode.getTextContent().equals("Alice Smith")) {

      }

    • 5). Give Alice Smith a new name. Place this code within the "if" statement from step 3:

      nameNode.setTextContent("Alice Wilson");

    • 6). Save the altered XML file to the disK:

      FileOutputStream fos = new FileOutputStream("C:\test.xml");

      OutputFormat of = new OutputFormat("XML","ISO-8859-1",true);

      XMLSerializer serializer = new XML11Serializer(fos,of);
      serializer.serialize(d.getDocumentElement());

      fos.close();

    • 7). Catch any exceptions or errors that may be generated if something goes wrong. Add the following code to the end of the try-block:

      catch (SAXException e) {
      e.printStackTrace();
      } catch (ParserConfigurationException e) {
      e.printStackTrace();
      } catch (IOException e) {
      e.printStackTrace();
      }

Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.