- From: Nigel Byrnes <byrnes@prl.research.philips.com>
- Date: Wed, 07 Apr 1999 15:03:53 +0100
- To: David Brownell <db@argon.Eng.Sun.COM>
- Cc: www-dom@w3.org, xml-feedback@java.sun.com
- Message-Id: <370B65C8.6A660C2D@prl.research.philips.com>
Hi Mike and David
Thanks for your replies. The sun DOM API doesn't allow me to do this
directly. But I managed to come up with a simple solution which does what is
required. The approach is (see setTextInNode(Node n, String newText)):
- make a clone of the text node which is to be modified
- modify the text value of the node
- replace this cloned node with the old one.
The code for this is below.
Thanks again
Nigel
======================= 8< ====================
public void copyIntoParent(Node n) {
// get current node's text content copy it
// into parent node, in place of current node
// Has the affect of removing elements from
// text content.
System.out.println("child " + n.getNodeName() + " " +
getTextFromNode(n));
setTextInNode(n, "** my new text **");
System.out.println("child " + n.getNodeName() + " " +
getTextFromNode(n));
}
public void setTextInNode(Node n, String newText) {
Node child = n.getFirstChild(),
clone;
final boolean deep = false;
// Read the text that this node has
// and append to it the new text that is to be added
String currentText = getTextFromNode(n);
currentText += newText;
clone = child.cloneNode(deep);
clone.setNodeValue(currentText);
n.replaceChild(clone, child);
}
public String getTextFromNode(Node n) {
// get current node's text content
Node child;
String textData = "";
NodeList children;
int numberOfChildren;
// If the node has any child nodes,
// bung all of them into a nodelist called children
children = n.getChildNodes();
numberOfChildren = children.getLength();
// Read each child now
for (int i = 0; i < numberOfChildren; i++) {
child = children.item(i);
// If this child has some text, let's get it!
if (child.getNodeType() == 3)
textData += child.getNodeValue();
// else
// System.out.println("No text with one child of " + child.getNodeName()
+ " Type: " + child.getNodeType());
}
return textData;
}
David Brownell wrote:
> > for example, ie, this xml document:
> >
> > <myDoc>this is some <emph>emphasis</emph></myDoc>
> >
> > gets transformed to:
> >
> > <myDoc>this is some emphasis</myDoc>
> >
> > The approach which I am using is:
> >
> > 1. Get the text associated with the <emph> node.
> > 2. append it to the text component of the <myDoc> node
> > 3. delete the <emph> node
>
> Simplest is to
>
> 1. Get the TEXT node child of the "emph" node.
> 2. Get the parent of that "emph" node.
> 2. Use parent.replaceChild() to replace the "emph" node with the text.
>
> > The trouble is with the setNodeValue(String) method in the Node
> > interface of Sun's API. I call it in the setTextInNode(Node n, String
> > newText) method, but the print outs I have in the copyIntoParent(Node)
> > method show that it has no affect.
>
> Right, DOM does not specify any behavior other than not throwing
> any exception in this case ... perhaps it's annoying, but that's
> how it's specified. (The only exception that can be thrown is for
> a read-only node; since that element isn't read-only, it mustn't
> be thrown there.)
>
> - Dave
Received on Wednesday, 7 April 1999 10:07:08 UTC