Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8551289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:09:51+00:00 2026-06-11T14:09:51+00:00

I want to modify an existing XML file using xPath. If the node doesn’t

  • 0

I want to modify an existing XML file using xPath. If the node doesn’t exist, it should be created (along with it’s parents if neccessary). An example:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <param0>true</param0>
  <param1>1.0</param1>
</configuration>

And here are a couple of xPaths I want to insert/modify:

/configuration/param1/text()         -> 4.0
/configuration/param2/text()         -> "asdf"
/configuration/test/param3/text()    -> true

The XML file should look like this afterwards:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <param0>true</param0>
  <param1>4.0</param1>
  <param2>asdf</param2>
  <test>
    <param3>true</param3>
  </test>
</configuration>

I tried this:

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

try {
  DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
  Document doc = domFactory.newDocumentBuilder().parse(file.getAbsolutePath());
  XPath xpath = XPathFactory.newInstance().newXPath();

  String xPathStr = "/configuration/param1/text()";
  Node node = ((NodeList) xpath.compile(xPathStr).evaluate(doc, XPathConstants.NODESET)).item(0);
  System.out.printf("node value: %s\n", node.getNodeValue());
  node.setNodeValue("4.0");

  TransformerFactory transformerFactory = TransformerFactory.newInstance();
  Transformer transformer = transformerFactory.newTransformer();
  transformer.transform(new DOMSource(doc), new StreamResult(file));
} catch (Exception e) {
  e.printStackTrace();
}

The node is changed in the file after running this code. Exactly what I wanted. But if I use one of the below paths, node is null (and therefore a NullPointerException is thrown):

/configuration/param2/text()
/configuration/test/param3/text()

How can I change this code so that the node (and non existing parent nodes as well) are created?

EDIT: Ok, to clarify: I have a set of parameters that I want to save to XML. During development, this set can change (some parameters get added, some get moved, some get removed). So I basically want to have a function to write the current set of parameters to an already existing file. It should override the parameters that already exist in the file, add new parameters and leave old parameters in there.

The same for reading, I could just have the xPath or some other coordinates and get the value from the XML. If it doesn’t exist, it returns the empty string.

I don’t have any constraints on how to implement it, xPath, DOM, SAX, XSLT… It should just be easy to use once the functionality is written (like BeniBela’s solution).

So if I have the following parameters to set:

/configuration/param1/text()         -> 4.0
/configuration/param2/text()         -> "asdf"
/configuration/test/param3/text()    -> true

the result should be the starting XML + those parameters. If they already exist at that xPath, they get replaced, otherwise they get inserted at that point.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T14:09:52+00:00Added an answer on June 11, 2026 at 2:09 pm

    If you want a solution without dependencies, you can do it with just DOM and without XPath/XSLT.

    Node.getChildNodes|getNodeName / NodeList.* can be used to find the nodes, and Document.createElement|createTextNode, Node.appendChild to create new ones.

    Then you can write your own, simple “XPath” interpreter, that creates missing nodes in the path like that:

    public static void update(Document doc, String path, String def){
      String p[] = path.split("/");
      //search nodes or create them if they do not exist
      Node n = doc;
      for (int i=0;i < p.length;i++){
        NodeList kids = n.getChildNodes();
        Node nfound = null;
        for (int j=0;j<kids.getLength();j++) 
          if (kids.item(j).getNodeName().equals(p[i])) {
        nfound = kids.item(j);
        break;
          }
        if (nfound == null) { 
          nfound = doc.createElement(p[i]);
          n.appendChild(nfound);
          n.appendChild(doc.createTextNode("\n")); //add whitespace, so the result looks nicer. Not really needed
        }
        n = nfound;
      }
      NodeList kids = n.getChildNodes();
      for (int i=0;i<kids.getLength();i++)
        if (kids.item(i).getNodeType() == Node.TEXT_NODE) {
          //text node exists
          kids.item(i).setNodeValue(def); //override
          return;
        }
    
      n.appendChild(doc.createTextNode(def));    
    }
    

    Then, if you only want to update text() nodes, you can use it as:

    DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
    Document doc = domFactory.newDocumentBuilder().parse(file.getAbsolutePath());
    
    update(doc, "configuration/param1", "4.0");
    update(doc, "configuration/param2", "asdf");
    update(doc, "configuration/test/param3", "true");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to modify an existing XML-File. I used a DataSet to load the
I want to modify an existing Perforce label using a script that I can
I have an XML file I'm trying to modify and want to add a
I want to change from one XML (XHTML) file to another using XSLT. In
I want to know I can dynamically modify an existing Crystal Report (using C#
How can I modify an existing *.gem file? I want to modify a Rakefile
I want to modify my existing folder structure. I had a file tree that
I writing a dynamic HTML parsers functionality. I will want to modify existing parsers
I want to modify the existing Android demo application 3D Rubik cube that uses
I need to modify an existing Perl program. I want to pipe a string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.