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. Advertisements

  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. Advertisements

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:
    458
    Tobi Krausl
    Nov 18, 2003
  2. Tony Prichard
    Replies:
    0
    Views:
    1,026
    Tony Prichard
    Dec 12, 2003
  3. Johannes Bauer
    Replies:
    7
    Views:
    1,269
    Johannes Bauer
    Jun 11, 2009
  4. arne
    Replies:
    0
    Views:
    496
  5. Erik Wasser
    Replies:
    5
    Views:
    864
    Peter J. Holzer
    Mar 5, 2006
  6. Sean
    Replies:
    3
    Views:
    614
    robic0
    Oct 3, 2006
  7. Sean
    Replies:
    0
    Views:
    504
  8. SMH
    Replies:
    2
    Views:
    450
Loading...