File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes JAXP Not Creating Nodes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "JAXP Not Creating Nodes" Watch "JAXP Not Creating Nodes" New topic
Author

JAXP Not Creating Nodes

Gareth Western
Ranch Hand

Joined: Apr 07, 2004
Posts: 45
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Apr 07, 2004
Posts: 45
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JAXP Not Creating Nodes