URGENT: Using DOM Parser for Save a XML document

A

Achille

Hi all,
I have a file test.xml with the following nodes:

<?xml version="1.0" encoding="ISO-8859-1"?>
<START>
<Profile1 hostname="host1" lastselected="ture"></Profile1>
<Profile2 hostname="host2" lastselected="ture"></Profile2>
<Profile3 hostname="host3" lastselected="ture"></Profile3>
</START>

and I want to change the value of attribute hostname (host1) in
Profile1 node.
I am using DOMParser and I used the following command:

node.getAttributes().item(0).setNodeValue("HostChanged");

in order to change the document in test.xml in:

<?xml version="1.0" encoding="ISO-8859-1"?>
<START>
<Profile1 hostname="HostChanged" lastselected="ture"></Profile1>
<Profile2 hostname="host2" lastselected="ture"></Profile2>
<Profile3 hostname="host3" lastselected="ture"></Profile3>
</START>

the command is ok but the DOM work in memory.

Can you help me to save the DOM tree changed in the file test.xml in
order to change the fisical file test.xml ?

Thanks!!
 
J

John C. Bollinger

Achille said:
Hi all,
I have a file test.xml with the following nodes:

<?xml version="1.0" encoding="ISO-8859-1"?>
<START>
<Profile1 hostname="host1" lastselected="ture"></Profile1>
<Profile2 hostname="host2" lastselected="ture"></Profile2>
<Profile3 hostname="host3" lastselected="ture"></Profile3>
</START>

and I want to change the value of attribute hostname (host1) in
Profile1 node.
I am using DOMParser and I used the following command:

node.getAttributes().item(0).setNodeValue("HostChanged");

in order to change the document in test.xml in:

<?xml version="1.0" encoding="ISO-8859-1"?>
<START>
<Profile1 hostname="HostChanged" lastselected="ture"></Profile1>
<Profile2 hostname="host2" lastselected="ture"></Profile2>
<Profile3 hostname="host3" lastselected="ture"></Profile3>
</START>

the command is ok but the DOM work in memory.

Can you help me to save the DOM tree changed in the file test.xml in
order to change the fisical file test.xml ?

Thanks!!

Use a default Transformer
(TransformerFactory.newInstance().newTransformer()). Feed the
transformer with a DOMSource based on your Document, and direct its
output to a StreamResult wrapping a suitable output stream. Read the
API docs for details.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top