File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "==> filtering XML file with LSSerializerFilter" Watch "==> filtering XML file with LSSerializerFilter" New topic

==> filtering XML file with LSSerializerFilter

Aimee Best

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();

LSSerializer serializer = impLS.createLSSerializer();

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

The OutputFilter is:

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

class OutputFilter implements LSSerializerFilter {

public int getWhatToShow() {
return NodeFilter.SHOW_ELEMENT;

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


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


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

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?

I agree. Here's the link:
subject: ==> filtering XML file with LSSerializerFilter
Similar Threads
help!Urgent,my assignment.....
a ReadXMLFile Servlet works without my expectation...
Problem serializing processing instruction with LSSerializer
XPath Problem
XPath Problem