Esteban Gonzalez wrote:
> Node attr =
> list.item(j);
> if(
> attr.getNodeName().equals("pais") )
>
> item.setNodeValue( "1000" );
> if(
> attr.getNodeName().equals("codigo") )
>
> item.setNodeValue( "jua jua jua");
Element nodes do not have a "value", they have children.
Therefore, calling get/setValue on an element node should
not do anything. In order to change the text appearing
within an element, you need to remove its children and
append a new text node child. For example:
if (attr.getNodeName().equals("pais")) {
Node itemChild = item.getFirstChild();
while (itemChild != null) {
item.removeChild(itemChild);
itemChild = item.getFirstChild();
}
Document doc = item.getOwnerDocument();
Node itemText = doc.createTextNode("1000");
item.appendChild(itemText);
}
Another option would be to just do a shallow clone of the
element, add your new text child, and replace the original
element node in the document. I don't really recommend this
general practice because you end up using more memory than
required. However, if you were a Windows programmer in your
previous life, then perhaps this doesn't bother you... ;)
Regardless, here's that code:
if (attr.getNodeName().equals("pais")) {
Node itemClone = item.cloneNode(false);
Document doc = item.getOwnerDocument();
Node itemCloneText = doc.createTextNode("1000");
itemClone.appendChild(itemCloneText);
item.getParentNode().replaceChild(itemClone, item);
}
A third option is to use a stylesheet to do the replacement
for you. However, this may be a bit of overkill depending
on how much document transformation you need to perform.
You can decide what's best for you.
A lot of people complain that these are the things they
hate about programming the DOM -- that is should provide
convenience methods to do this sort of thing. If it bugs
you that much, then you might find DOM4J or JDOM more to
your liking.
However, the DOM is a W3C standard which counts for a lot
-- it's a generic interface that can be used to hook up
different applications or differents parts of your single
application. And converting back and forth between tree
models is usually a waste of memory.
So, if you stick with the DOM, you may find it beneficial
to start collecting a number of static utility methods in
a class to ease your programming burden. We've done that
ourselves with the org.apache.xerces.util.DOMUtil class.
I just checked and it looks like we don't have any methods
to get/set an element's text value. But we should because
that would be super useful.
--
Andy Clark * [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]