W3C home > Mailing lists > Public > www-dom@w3.org > April to June 1999

Re: Modifying the text component of a DOM Node

From: Mike Champion <mike.champion@aliaron.com>
Date: Tue, 6 Apr 1999 09:27:54 -0400
Message-ID: <001b01be8031$485c1660$54ac45d1@mcc.aliaron>
To: "Nigel Byrnes" <byrnes@prl.research.philips.com>, <www-dom@w3.org>
Cc: <xml-feedback@java.sun.com>
The DOM Level 1 defines the nodeValue of an Element to be null (see the
table in the discussion of the Node interface), so your approach wouldn't
work in the DOM standard.  It is, however, a source of irritation and
confusion for many people, and some DOM implementers have defined a method
similar to setNodeValue to do what you want to do with an Element.  It's
hard to come up with a clean, elegant solution here without adding
serialization and parsing interfaces to the DOM, and that is considered by
many to be a can of worms.

The "DOM-ically correct" way strip out the <emph> markup, I believe, is to
get the text nodes of "myDoc" and "emph" and concatenate them together,
remove the "emph" element, then set the new Text node to be the only child
of "myDoc".

Mike Champion

-----Original Message-----
From: Nigel Byrnes <byrnes@prl.research.philips.com>
To: www-dom@w3.org <www-dom@w3.org>
Cc: xml-feedback@java.sun.com <xml-feedback@java.sun.com>
Date: Tuesday, April 06, 1999 7:23 AM
Subject: Modifying the text component of a DOM Node


>Hi DOM-ers
>
>I have a problem with modifying the text component of a node. What I am
>hoping to achieve could have the affect of removing emphasis from text,
>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
>
>Using Sun's Java xml-ea2 DOM API
>(http://developer.javasoft.com/developer/earlyAccess/xml/index.html),
>check out the following code for this purpose. In the example above, the
>method copyIntoParent (Node) would be called with <emph> as the
>parameter.
>
>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.
>
>So:
>- are there any other DOM-ers using Suns DOM API?
>- is setNodeValue(String) the right method to call. if not, what?
>
>
>Any help/ suggestions will be cool
>
>thanks
>
>N
>
>
>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.
>
>  // Print out the current text value of this node
>  System.out.println("child " + n.getNodeName() + "  " +
>getTextFromNode(n));
>
>  // update the text value of this node with the "** my new text **"
>  setTextInNode(n, "** my new text **");
>
>  // Print out the updated text value of this node
>  System.out.println("child " + n.getNodeName() + "  " +
>getTextFromNode(n));
>
> }
>
> public void setTextInNode(Node n, String newText) {
>
>  // 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;
>
>  n.setNodeValue(currentText);
> }
>
>
> 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();
>  }
>  return textData;
> }
>
>
>
Received on Tuesday, 6 April 1999 09:28:32 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Friday, 22 June 2012 06:13:46 GMT