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 how to update the attribute value 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 "how to update the attribute value" Watch "how to update the attribute value" New topic
Author

how to update the attribute value

ajse ruku
Ranch Hand

Joined: May 06, 2005
Posts: 194
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

Joined: Apr 14, 2004
Posts: 10336

Can you show us what you tried? Maybe someone will spot where you made your mistake.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12781
    
    5
Note that after modifying the attribute you have to write the entire DOM back out to the file.
Bill
ajse ruku
Ranch Hand

Joined: May 06, 2005
Posts: 194
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

Joined: Mar 22, 2000
Posts: 12781
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to update the attribute value