• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to update the attribute value

 
ajse ruku
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
My xml file looks like this.
<program>
<client host="192.168.1.34">
</client>
</program>
I want to update the value of host attribute to 192.168.1.66.How to do this in java.I have tried using DOM but it is not working.Can somebody have any idea?

with regards,
ajse
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show us what you tried? Maybe someone will spot where you made your mistake.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that after modifying the attribute you have to write the entire DOM back out to the file.
Bill
 
ajse ruku
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks William,
You are correct.I think I am not writing back the dom structure.How should I do this.Which API should I use for this?
with regards,
ajse
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have Java 1.4 or 1.5 you will be using the javax.xml.transform.Transformer class and related classes.
Here is an example, where baos is an instance variable:

Instead of the ByteArrayOutputStream this could be writing to a FileOutputStream. The newTransformer() call creates a Transformer that does not change the DOM, other Transformers can apply a XSL transformation.

Welcome to the weird world of XML with Java - Sun has a pretty good tutorial, just google for "Sun XML tutorial"

Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic