473,750 Members | 2,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SAX XMLReader, XMLFilter, ContentHandler and XMLWriter question

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

Feb 22 '06 #1
2 2228
I forgot to add that I don't understand what to do with the
ContentHandler class;
I tried to use the DefaultHandler, and then I tried my own class
"JeffContentHan dler"
with an empty implementation. It seems to me that the Filter class is
doing this
work though, so why would I register a ContentHandler?

--Jeff

Feb 22 '06 #2
Jeff Calico wrote:
I forgot to add that I don't understand what to do with the
ContentHandler class;
I tried to use the DefaultHandler, and then I tried my own class
"JeffContentHan dler"
with an empty implementation. It seems to me that the Filter class is
doing this
work though, so why would I register a ContentHandler?


Normally, the filter is a ContentHandler whose only job is to pass
selected events along to another ContentHandler which actually uses the
filtered document. You have to register your "real" ContentHandler with
the filter so it knows what to do with the events after deciding whether
to keep them or not.

Alternatively, of course, you can combine both the filtering and the
operate-on-the-data stages in a single custom ContentHandler. But in
that case there's no need for it to claim to be a Filter.
--
Joe Kesselman / Beware the fury of a patient man. -- John Dryden
Feb 22 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1329
by: Shivonne | last post by:
Hi all Having problems with the XmlReader object. I'm trying to transform an XML document with XSLT - and get the resulting output. My code is as follows: XsltArgumentList xslArgs = passParameters (xmlMenu.SelectSingleNode("document/setup")); // just a local function to get all parameters in the doc XslTransform xslTransform = new XslTransform();
1
2622
by: Fede | last post by:
Hi to all! someone of you know the difference between XMLWriter/XMLReader and MSXML4.0? The first one has the same capacity of the second one? Which is better to use? Which give the clearest messages or the greatest amount of information about the kind and the origin of error (considering that I have to show messages in Italian and the Framework is in English)? And ...is it difficult to include msxml4.dll in my project? Thank you in...
1
1798
by: Philipp Schumann | last post by:
Hi, I have the following code to copy nodes from an XML document (XmlReader reader) to some output (XmlWriter writer). while (reader.Read ()) if (reader.MoveToContent () == XmlNodeType.Element) break; // forward the reader to the document element writer.WriteStartDocument (false);
3
8044
by: Michael Malinak | last post by:
Since XmlWriter offers so many nice options for formatting, I thought it would be nice to read in via XmlReader, and write back out via XmlWriter. It might be overkill, but I'd also like to be able to check some values during that time also so I was going to be using XmlReader anyway. Unfortunately I don't see an easy way to stream it back out through XmlWriter without going node by node. Any suggestions? Is there an easier/faster way...
8
2490
by: Charles.Deisler | last post by:
Im currently using the following code.. XmlDocument xmlData = new XmlDocument(); XmlTextWriter xmlwriter = new XmlTextWriter(Response.OutputStream,System.Text.Encoding.UTF8); XmlReader xmlreader = SqlHelper.ExecuteXmlReader(...) xmlData.Load(xmlreader); xmlData.WriteTo(xmlwriter); xmlwriter.Flush();
2
17201
by: muesliflakes | last post by:
I wish to receive some data from SqlServer as XML and write out result to as a string. Eg: cmd = new SqlCommand( "select * from blah for xml auto", con ); XmlReader result = cmd.ExecuteXmlReader( ); Console.Writeln( result.ToString( ) ); This XML is meant to be the input for an XSLT transform so I'm
2
3904
by: Amit | last post by:
I have assigned a simple xml into a XmlReader. A switch case loops through the nodes and writes the data to XmlWriter object which is initialized to a xml file. The output xml file is correctly generated and everything works fine. But if the input xml has an & then I am unable to generate a valid output xml file. I have pasted the code below: public class Class1 {
0
1735
by: jrd | last post by:
Hello, I am very new to PHP. What I need to do is read an XML file (RSS feed) into memory, iterate through the file changing one particular value for each entry, and write a new XML file with a different name. I have a pretty good understanding of how to use xmlreader to read the XML file and get at the values for each item in the feed. What I'm wondering is, can I use xmlwriter simultaneously with xmlreader to modify one of the...
2
5590
by: SammyBar | last post by:
Hi all, I'm trying to convert the xml obtained from a XmlReader object into a UTF-8 array. My general idea is to read the XmlReader and write into a MemoryStream. Then convert the MemoryStream bytes into utf-8. MemoryStream ms = new MemoryStream(); XmlTextWriter xmlWriter = new XmlTextWriter(ms, new UTF8Encoding(false)); writer.Formatting = Formatting.Indented;
0
9394
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9256
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6803
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3322
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2223
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.