URGENT: Using DOM Parser for Save a XML document

Discussion in 'Java' started by Achille, Oct 19, 2004.

  1. Achille

    Achille Guest

    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!!
    Achille, Oct 19, 2004
    #1
    1. Advertising

  2. Achille wrote:

    > 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
    John C. Bollinger, Oct 19, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tobi Krausl
    Replies:
    0
    Views:
    355
    Tobi Krausl
    Nov 18, 2003
  2. Tony Prichard
    Replies:
    0
    Views:
    708
    Tony Prichard
    Dec 12, 2003
  3. arne
    Replies:
    0
    Views:
    339
  4. Sean
    Replies:
    3
    Views:
    246
    robic0
    Oct 3, 2006
  5. SMH
    Replies:
    2
    Views:
    242
Loading...

Share This Page