aspose file tools*
The moose likes XML and Related Technologies and the fly likes JDOM adding new elements to existing XML documents 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 "JDOM adding new elements to existing XML documents" Watch "JDOM adding new elements to existing XML documents" New topic
Author

JDOM adding new elements to existing XML documents

Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

I am a brand new to coderanch, so I am sorry if i break any styling-rules. I am working with JDOM in order to create my primary save data. I want to have 1 document for everything. In order to do that, I have created a model for an XML page. Initially at "program installation" I want to have a blank XML document with only the <program> tag. Later, I want to be able to create new users. <user name="user1" password="theirPassword"> I want to be able to add a new user later on so that the date would look like:

<program>
<user --attributes>
<otherTags/>
<user -- attributes>
<otherTags/>
</program>

my issue:

i can open the documen using SAXBuilder.build(). when I use XMLOutputter, it just rewrittes everything, and only what I just added remains.

my Question:

How would I write the the method (using JDOM) that takes the paramaters String UserName and String Password and saves it into an already existing XML document?

Thanks in advance
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

So, reading between the lines, it looks like you have some code which uses JDOM to parse an XML document into a JDOM Document object. And you're asking how to write code to change that Document by adding new elements. Well, to find that out I would recommend reading a JDOM tutorial. I used the google keywords "jdom tutorial" and the first link I got back was this one: Easy Java/XML integration with JDOM. It looks like Part 2 directly addresses your specifc question, so good luck! And welcome to the Ranch!
Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

Thanks, that was a good resource, but still did not really help me solve my current problem. Here is my code as it now is. (the formatting is a little off because I copied and pasted only parts of the code.)




do you have any idea what I am doing wrong, and why the initial information is being erased? Am I just being stupid and need to use 'user' as a namespace and 'userChosenName' as the element tag? I have already spent hours on google searching without results to this particular issue.

Thanks Greg
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18911
    
    8

Without seeing the input and the output it's pretty difficult to figure out what might be the problem.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 544
    
    3
>Am I just being stupid and need to use 'user' as a namespace and 'userChosenName' as the element tag?
That is not the problem. Sometimes we rely on some kind of imagination on the cause of a problem only to find it harder to unlearn the experience of wrong diagnosis. Namespace and tag name have nothing to do with the problem.

>do you have any idea what I am doing wrong, and why the initial information is being erased?
This is the problem and how to rectify it. Since you want to first read the data from a file and then update the same file, your writer and reader are actually instantiated with the same file path. Here pose a problem. If you instantiate writer immediately before or after the reader being instantiated, the file would thereby be empty. Hence reader's functioning will be adversely interferred. This is how you rectify the code preserving the general structure of it.

[1] Comment out the line #12. Just not to instantiate the writer, yet.


[2] Add these between lines #26 and #27.

Then you should find the file being updated preserving the existing data plus the new.
Greg Reeder
Ranch Hand

Joined: Jun 14, 2011
Posts: 99

Thanks a lot. That was it!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JDOM adding new elements to existing XML documents