Currently I'm trying to develop some code with Java 9 and javax.xml libraries (both mandatory for my task) that edits an XML file and I'm having some weird issues adding child nodes.
This is the XML file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<users>
</users>
and I want to edit it build something like this:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<users>
<user>
<name>A name</name>
<last-name>Last Name</last-name>
<username>username</username>
</user>
</users>
Now, the first run of the code adds a single blank line before the <user> node. When it runs for a second time fills with more blank lines:
<users>
<user>
<name>name</name>
<last-name>lastname</last-name>
<username>username</username>
</user>
<user>
<name>name</name>
<last-name>lastname</last-name>
<username>username</username>
</user>
</users>
This is the XML generated after running the program 2 times. As you can see, it adds blank lines before the <user> nodes and between the other nodes, exactly n-1 blank lines between nodes being n the times the code was executed.
Wondering what is the content of those nodes before updating the file I wrote the next code:
int i=0;
while (root.getChildNodes().item(i)!=null){
Node aux = root.getChildNodes().item(i);
System.out.println("Node text content: ".concat(aux.getTextContent()));
i++;
}
1st execution:
Node text content:
Node text content: namelastnameusername
2nd execution:
Node text content:
Node text content:
name
lastname
username
Node text content:
Node text content: namelastnameusername
3rd execution
Node text content:
Node text content:
name
lastname
username
Node text content:
Node text content:
name
lastname
username
Node text content:
Node text content: namelastnameusername
Finally, this is the Java code:
private static void saveUser(String firstName, String lastName, String username){
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new File(databaseFile));
Element root = doc.getDocumentElement();
root.normalize();
// build user node
Element userNode = doc.createElement("user");
Element nameNode = doc.createElement("name");
Element lastNameNode = doc.createElement("last-name");
Element usernameNode = doc.createElement("username");
//build structure
nameNode.appendChild(doc.createTextNode(firstName));
lastNameNode.appendChild(doc.createTextNode(lastName));
usernameNode.appendChild(doc.createTextNode(username));
userNode.appendChild(nameNode);
userNode.appendChild(lastNameNode);
userNode.appendChild(usernameNode);
root.appendChild(userNode);
//write the updated document to file or console
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(new File(databaseFile));
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(source, result);
}catch (SAXException | ParserConfigurationException | IOException | TransformerException e1) {
e1.printStackTrace();
}
}
The only solution I could find is to delete blank lines after XML generation, but I think it's not a proper solution and I would like to find some alternatives first.
Any suggestions on how to tackle this problem?