Hello all. I am implementing a SAX filter to strip a bunch of unneeded
elements out of a large XML file. I found a book "Java & XML" by Brett
McLaughlin, and an interesting article by him wich address my issues:
http://www-128.ibm.com/developerwork...ipbigdoc3.html
However, doing it in that specified way does not seem to work at all!
Doing it very differently *seems* to work, but most likely I am not
understanding something.
Here is my code, contrasted with the book's code. The class
KeepSpecificEle mentsFilter is at the end:
--------------------------
MY TRIAL AND ERROR WAY:
---------------------------
FileReader r = new FileReader( "filename" );
XMLReader xr = XMLReaderFactor y.createXMLRead er();
KeepSpecificElt sFilter filter = new KeepSpecificElt sFilter( xr,
"elt");
XMLWriter xw = new XMLWriter( filter, new FileWriter( "Out.xml" ) );
xw.parse( new InputSource(r) );
--------------------------------------------------------
THE WAY THE BOOK SAYS TO DO IT (maybe I misunderstand):
---------------------------------------------------------
FileReader r = new FileReader( s );
XMLReader xr = XMLReaderFactor y.createXMLRead er();
XMLWriter xw = new XMLWriter( xr, new FileWriter( "jeffOut.xm l" ) );
KeepSpecificElt sFilter filter = new KeepSpecificElt sFilter( xw,
"elt");
//DefaultHandler dh = new DefaultHandler( );
JeffContentHand ler dh = new JeffContentHand ler(xr);
filter.setConte ntHandler( dh );
filter.parse( new InputSource(r) );
------------------------------------------------------------
Note the difference between who does the parsing
(writer or filter) and the way they are chained together.
And Last, here is the Filter class:
------------------------------------------------------------
public class KeepSpecificElt sFilter extends XMLFilterImpl {
private List elementsToKeep;
private boolean inKeptElement = false;
public KeepSpecificElt sFilter( XMLReader parent, String
elementToKeep )
{
super( parent );
elementsToKeep = new LinkedList();
elementsToKeep. add(elementToKe ep);
}
//---------------------------------------------------------------------------
public KeepSpecificElt sFilter( XMLReader parent, List elementsToKeep
)
{
super(parent);
this.elementsTo Keep = elementsToKeep;
}
//---------------------------------------------------------------------------
public void startElement( String uri, String localName, String qName,
Attributes atts)
throws SAXException
{
if( elementsToKeep. contains(localN ame) )
{
System.out.prin tln("In kept element = " + localName);
super.startElem ent( uri, localName, qName, atts );
inKeptElement = true;
}
else
{
}
}
//---------------------------------------------------------------------------
public void endElement( String uri, String localName, String qName )
throws SAXException
{
if( elementsToKeep. contains(localN ame) )
{
super.endElemen t( uri, localName, qName );
inKeptElement = false;
}
else
{
// DON'T DO ANYTHING... PREVENTS PROCESSING OF
ELEMENTS
}
}
//---------------------------------------------------------------------------
public void characters( char ch[], int start, int len )
throws
SAXException
{
if( inKeptElement )
{
super.character s( ch, start, len );
}
}
}
Any insight would be appreciated!
--Jeff