0

I am creating a simple java function to update/re-write the value of an xml file. I am able to pick the XML file from the system resource, after updating the file and re-writing, the value of the node never gets changes.

Here are my snippets:

String filePath = ABS_PATH + File.separator + "fields.xml";

File xmlFile = new File(filePath);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder;
try {
    dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(xmlFile);
    doc.getDocumentElement().normalize();

    //update Element value
    updateElementValue(doc);

    //write the updated document to file or console
    doc.getDocumentElement().normalize();
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();

    DOMSource source = new DOMSource(doc);
    StreamResult result = new StreamResult(new File(ABS_PATH
                 + File.separator + "fields.xml")); // updating/re-writing the same file
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.transform(source, result);
    System.out.println("XML file updated successfully");

} catch (SAXException | ParserConfigurationException | IOException | TransformerException e1) {
    e1.printStackTrace();
}

//method to show the update element value
private static void updateElementValue(Document doc) {
    NodeList employees = doc.getElementsByTagName("NO");
    Element emp = null;
    //loop for each
    for (int i = 0; i < employees.getLength(); i++) {
        emp = (Element)employees.item(i);
        Node name = emp.getElementsByTagName("String").item(0).getFirstChild();
        name.setNodeValue(name.getNodeValue().toUpperCase());
    }

}

sample of the xml file

  <Document xmlns="http://hello.com/schema/public/services/platform" Id="1">
  <Fields>
    <Field FieldName="NO">
      <String>Demo</String>
    </Field>
    <Field FieldName="TYPE">
      <String>Zada</String>
    </Field>
  </Fields> 

who is motivated enough to assist

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Blaze
  • 2,269
  • 11
  • 40
  • 82

1 Answers1

0

You have the following statement:

 NodeList employees = doc.getElementsByTagName("NO");

It will return an empty list, since your XML does not have any nodes with "NO" as a tag.

 <Field FieldName="NO">

Statements inside your loop iterating through the list will never be executed. You node will not be updated.

Your elements with tag Field have attribute FieldName. One of them has attribute value of "NO". So you need to find it.

See if How to get specific XML elements with specific attribute value? gives you enough guidance.

You can do it with a loop as well.

NodeList employees = doc.getElementsByTagName("Field");
for (int i = 0; i < employees.getLength(); i++) {
     emp = (Element)employees.item(i);
     if ("NO".equals(emp.item(i).getAttribute("FieldName"))) {
           // do your stuff
     }
}

The above is not tested.

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • thanks, but how do I get this - FieldName="NO" to get this value - Demo and update it. Thanks – Blaze Feb 13 '20 at 17:49
  • Updated my answer. – PM 77-1 Feb 13 '20 at 18:11
  • Are you familiar with [XPATH](https://docs.oracle.com/javase/tutorial/jaxp/xslt/xpath.html)? – PM 77-1 Feb 13 '20 at 18:33
  • Well.. Either you familiarize yourself or use a library like [JSOUP.](https://jsoup.org/apidocs/org/jsoup/nodes/Element.html#getElementsByAttributeValue-java.lang.String-java.lang.String-). – PM 77-1 Feb 13 '20 at 18:37
  • I followed thru this tutorial, as it has same structure with mine, but no head-way https://www.journaldev.com/901/modify-xml-file-in-java-dom-parser – Blaze Feb 13 '20 at 18:37
  • OK. Posted it using only functionality you already should be familiar with. – PM 77-1 Feb 14 '20 at 02:04