aspose file tools*
The moose likes XML and Related Technologies and the fly likes ==> filtering XML file with LSSerializerFilter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "==> filtering XML file with LSSerializerFilter" Watch "==> filtering XML file with LSSerializerFilter" New topic
Author

==> filtering XML file with LSSerializerFilter

Aimee Best
Greenhorn

Joined: Feb 27, 2010
Posts: 4
Hello there,

I am trying to filter an xml file while serializing with LSSerializer. Here is a bit of code to explain what I am trying to do:

Let's say we have a document already:
Document d = parser.parse(input);

LSOutput output = impLS.createLSOutput();
output.setByteStream(System.out);

LSSerializer serializer = impLS.createLSSerializer();

serializer.setFilter( new OutputFilter() );
serializer.write(d, output);



The OutputFilter is:

import org.w3c.dom.Node;
import org.w3c.dom.ls.LSSerializerFilter;
import org.w3c.dom.traversal.NodeFilter;

class OutputFilter implements LSSerializerFilter {

@Override
public int getWhatToShow() {
return NodeFilter.SHOW_ELEMENT;
}

@Override
public short acceptNode(Node n) {
if ( n.getNodeName().equals("isbn") ) return NodeFilter.FILTER_REJECT;
if ( n.getNodeName().equals("price")) return NodeFilter.FILTER_REJECT;
return NodeFilter.FILTER_ACCEPT;
}

} // end OutputFilter


Now comes the problem:

I expected elements like

<price>14.95</price>

to be "completely" filtered out, ie disappear entirely.
However, I get

14.95

I don't get the child node removed even though FILTER_REJECT
is supposed to do that in contrast to FILTER_SKIP.

What am I doing wrong?

Thanks in advance.

Aimee Best
Greenhorn

Joined: Feb 27, 2010
Posts: 4
Never mind, I was using xerces 2.9.1 and Java 6 update 18. I removed the xerces jar files and now it works as expected!

What is the default implementation of DOM Level 3 API in Java 6?

 
 
subject: ==> filtering XML file with LSSerializerFilter