• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Adding elements to an existing XML in java

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a xml file that looks like this:

<list>
<student>
<firstname>"a"</firstname>
<lastname>"b"</lastname>
</student>
</list>

The code I used to create this xml file is:




How do I add another student without replacing the first one?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After line 15, you already have the first <Student> element in your document, right? So this would be a good place to put your code which adds another <Student> element.

If you're concerned about reusing your student variable (and the others), you don't have to be. The data which they referred to is now safely referred to by the Document in some way, so you can reuse them.

And if you find you need to write many <Student> elements, you might want to have a loop which does that.
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And, welcome to the Ranch!
 
Angela Rizo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.

Yes, but I don't know the exact number of students I will need.
I need this for a gui, so the person who will use it can enter any number of students.
So when I call this method I don't want it to create a new xml file, but to add a new student to the existing one.

How can I write a method that only adds elements to the file, and doesn't recreate it?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In that case, you could start by reading the existing file into a Document object. Then you'd have to do something like this:

- Find the <list> element from the Document

- When the user enters data for a new student, create a <student> element and append it to the <list> element. (That last part corresponds to code you already have.)

- When the user is finished entering data, write the Document out to the file. (Again, just like you already did.)

You'll find that code dealing with Document objects is a bit tedious to write, but conceptually it mostly isn't difficult.
 
Angela Rizo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your help
Can you please explain how to make the for loop for adding elements to the xml file? Because i understand that even though the tag can be the same, every element has to have a different name.
So how do I create a new element name (for example firstname1, firstname2) every time a new student is created?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No!!! You absolutely should not use different element names for each student! I don't know where you heard that but it's a bad idea.

As for the loop, you don't need a loop. Just add a student element to the Document whenever the user asks you to do that.
 
Angela Rizo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But like I said, if i call the method evert time the user adds a new student, it creates a new xml file every time so the last file contains only the last student that was created.
Is there a way to make one method that will create the file, and another that will add elements to it?
 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I did that already, where I gave you an outline of how to do it.

But this DOM programming is hard to understand for a beginner, so let me give you some homework.

You have an XML file of the right format, so first write a program which reads it and prints the student info from it.

Next, change that program so it reads the file, adds a student to the DOM, then writes it out to the file.

Once you have that done you should have a better idea of what to do.
 
The fastest and most reliable components of any system are those that are not there. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic