Search code examples
javatextspecial-characters

How to change the specific line in an XML file?


I have a really long config file(not my own) and I want to change a specific line in it.

I need to change the ServerPort value to 12345 for example, but without the code knowing the actual number..(the 26900).

this is a very small part of the config file:

<?xml version="1.0"?>
<ServerSettings>
  <property name="ServerPort"               value="26900"/>             <!-- Port you want the server to listen on. -->
  <property name="ServerIsPublic"           value="true"/>              <!-- Should this server register to master server -->
  <property name="ServerName"               value="My Game Host"/>      <!-- Whatever you want the name to be. -->
  <property name="ServerPassword"           value=""/>                  <!-- Password to gain entry to the server -->
  <property name="ServerMaxPlayerCount"     value="8"/>                 <!-- Maximum Concurrent Players -->
  <property name="ServerDescription"        value="A 7 Days to Die server"/> <!-- Whatever you want the description to be. -->
  <property name="ServerWebsiteURL"         value=""/>                  <!-- Website URL for the server -->

Solution

  • The simple answer is use XML parser and change the desired attribute value. To search for node - use XPath.

    Let's take your XML file:

    xmlfile.xml

    <ServerSettings>
      <property name="ServerPort"               value="26900"/>             <!-- Port you want the server to listen on. -->
      <property name="ServerIsPublic"           value="true"/>              <!-- Should this server register to master server -->
      <property name="ServerName"               value="My Game Host"/>      <!-- Whatever you want the name to be. -->
      <property name="ServerPassword"           value=""/>                  <!-- Password to gain entry to the server -->
      <property name="ServerMaxPlayerCount"     value="8"/>                 <!-- Maximum Concurrent Players -->
      <property name="ServerDescription"        value="A 7 Days to Die server"/> <!-- Whatever you want the description to be. -->
      <property name="ServerWebsiteURL"         value=""/>                  <!-- Website URL for the server -->
    </ServerSettings>
    

    For example using DOM parser and using XPath to get the node

        File file = new File("xmlfile.xml");
        DocumentBuilder dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document xml = dBuilder.parse(file);
        XPath xpath = XPathFactory.newInstance().newXPath();
        Node portNode = (Node) xpath.compile("/ServerSettings/property[@name='ServerPort']").evaluate(xml, XPathConstants.NODE);
        portNode.getAttributes().getNamedItem("value").setNodeValue("8080");
    
        // Saving changed XML back
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        Result output = new StreamResult(new File("xmlfile.xml"));
        Source input = new DOMSource(xml);
        transformer.transform(input, output);
    

    will change from 26900 to 8080.
    If your file is really big (like tens of megabytes) better use SAX parser or try to search for it using regular expression. If it's regular settings file worth couple kilobytes - code above is the most simple.