This week's book giveaway is in the Cloud/Virtualizaton forum.
We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!
See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JAXP Not Creating Nodes

 
Gareth Western
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm trying to write a class to append/remove nodes from an XML file (which is passed as an argument and created if it does not already exist). Now I've done everything correctly according to every tutorial i've read, however for some reason the new information is still not written (neither in memory nor to the file, as far as i can tell).
I've tried using an XML file that already exists (and is both readable and writable), just as a trial test, and my class reports that it has parsed the XML and gets the root element... but it just doesn't add the new nodes.
Here's a snippet of what i'm doing to add the elements:
// Get the root
Element root = document.getDocumentElement();

// Then build the file node
Element newFile = document.createElement("file");
newFile.setAttribute("name", fileName);

// Build the file node properties
Element newSize = document.createElement("size");
Text sizeText = document.createTextNode(Integer.toString(size));
newSize.appendChild(sizeText);
newFile.appendChild(newSize);
// Append the file to root
root.appendChild(newFile);
Is there anything wrong there?
I've also just noticed that when I try to print the document information using System.out.println(root) it just outputs [dir: null] even when values have been hardcoded into the XML document. So maybe the code is NOT parsing the document correctly after all?
The (hard coded) XML document looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<dir>
<file name="foo.txt">
<size>100</size>
</file>
</dir>
Does anyone have any idea where something could be going wrong? I've probably forgot to include some information so just let me know if there are any more questions. Many thanks!
~A Frustrated Gareth
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gareth Western:
I've also just noticed that when I try to print the document information using System.out.println(root) it just outputs [dir: null] even when values have been hardcoded into the XML document. So maybe the code is NOT parsing the document correctly after all?

The org.w3c.dom.Document interface does not require the toString() method to return an XML string corresponding to the document -- you need to use something like this in order to see the document's structure.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gareth Western:
I've tried using an XML file that already exists (and is both readable and writable), just as a trial test, and my class reports that it has parsed the XML and gets the root element... but it just doesn't add the new nodes.
Are you perhaps forgetting to write the document back to disk? (calling appendChild() doesn't affect the file on your harddrive, only the in-memory DOM tree)
 
Gareth Western
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh yes! Thank you very much (for both answers)!
Obviously it's not writing it back to disk... silly me
As for the toString bit... I blindly assumed that the examples I read were correct, for they used node.toString and claimed it to output the entire tree. Oops.
All fixed now... cheers!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic