Here's a page you should check out...

http://www.w3.org/TR/DOM-Level-2-Core/java-binding.html

It's the W3C page describing the interface to not only traversing DOM trees,
but adding, modifying, and removing children.

Text that is found in tags are nodes just like the elements (except instead
of being ELEMENT_NODE type they're TEXT_NODE type).  You must first create
the text node, and then append it to the element you desire as a child.  A
brief example follows:

<example>
Document myDoc = new DocumentImpl ();
Element myNode = myDoc.createElement ("myElement");
Node myText = myDoc.createTextNode ("This is some text.");
myNode.appendChild (myText);
myDoc.appendChild (myNode);
</example>

The resulting tree would look like:
<myElement>This is some text.</myElement>

As far as your needs are concerned, you simply need to keep track of the
elements you want to change as you traverse the tree (probably in a Vector
or something) and iterate over the list making your additions/deletions
after you're finished iterating over the tree.  The reason for this is that
DOM trees (at least in my experience) do not like to be traversed as they're
being modified.

Good luck!
Brion Swanson

-----Original Message-----
From: Ren� Jensen [mailto:[EMAIL PROTECTED]
Sent: Thursday, March 01, 2001 8:27 AM
To: [EMAIL PROTECTED]
Subject: How do I put value into an element tag?


Hi

I know how to parse a xml-document into a DOM-tree, and traverse this tree.
Now I have 2 questions

1) I wanna fill out the empty tags i might have fx.

<root>
  <tag1></tag1>
</root>

should be:

<root>
  <tag1>Some new text here</tag1>
</root>

2) I wanna add childelements to my excisting elements fx.

<root>
  <tag1></tag1>
</root>

becomes:

<root>
  <tag1>
    </tag2>
    </tag3>
  </tag1>
</root>

I wanna do this in the DOM-tree, because I have to do it several times,
when my tree is done, I can return the tree (as a xml-document) with all the

data and new tags...

I have searched the net, but I can only find examples of how to traverse the

tree, but not a single example of how to add a new branch to my DOM-tree

Any help / hint would be appriciated.
If you have an example I would be very glad.

       _\|/_
       (@ @)
---oOOo-(_)-oOOo---
    Ren� Jensen
 [EMAIL PROTECTED]

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to