This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JDOM adding new elements to existing XML documents

 
Greg Reeder
Ranch Hand
Posts: 99
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20185
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20185
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without seeing the input and the output it's pretty difficult to figure out what might be the problem.
 
g tsuji
Ranch Hand
Posts: 649
3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>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
Posts: 99
Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. That was it!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic