W
Wired Earp
I've had some luck using string values "\t" "\n" and "\r" to insert tabs,
newlines and carriagereturn textnodes into a document, but I can't *read*
these nodes, at least not by analyzing the nodeValue. Am i missing
something?
/**
* NodeFilter supposed to remove ignorable whitespace
*/
private class WhiteSpaceFilter implements NodeFilter {
public short acceptNode ( Node node ) {
// HELLO?
String value = node.getTextContent ();
boolean ok = value.equals ( "\n" ) || value.equals ( "\t" );
return ok ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
}
}
/**
* Strip whitespace
* @param element DOMElement
*/
private void strip ( Element element ) {
List<Node> list = new ArrayList<Node> ();
NodeFilter filter = new WhiteSpaceFilter ();
Document document = element.getOwnerDocument();
DocumentTraversal traversable = (DocumentTraversal) document;
TreeWalker walker = traversable.createTreeWalker (
element, NodeFilter.SHOW_TEXT, filter, true );
while ( walker.nextNode() != null )
list.add ( walker.getCurrentNode ());
for ( Node node : list )
node.getParentNode().removeChild ( node );
}
newlines and carriagereturn textnodes into a document, but I can't *read*
these nodes, at least not by analyzing the nodeValue. Am i missing
something?
/**
* NodeFilter supposed to remove ignorable whitespace
*/
private class WhiteSpaceFilter implements NodeFilter {
public short acceptNode ( Node node ) {
// HELLO?
String value = node.getTextContent ();
boolean ok = value.equals ( "\n" ) || value.equals ( "\t" );
return ok ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT;
}
}
/**
* Strip whitespace
* @param element DOMElement
*/
private void strip ( Element element ) {
List<Node> list = new ArrayList<Node> ();
NodeFilter filter = new WhiteSpaceFilter ();
Document document = element.getOwnerDocument();
DocumentTraversal traversable = (DocumentTraversal) document;
TreeWalker walker = traversable.createTreeWalker (
element, NodeFilter.SHOW_TEXT, filter, true );
while ( walker.nextNode() != null )
list.add ( walker.getCurrentNode ());
for ( Node node : list )
node.getParentNode().removeChild ( node );
}