You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Mansour Al Akeel <ma...@gmail.com> on 2011/01/09 21:30:18 UTC

Sax events writer

Hello all,

I am having a little issue trying to write the resutls of SAX event to a file.
In the past I have done this through diffent ways, like using Xalan:

   Serializer serializer = SerializerFactory
.getSerializer(OutputPropertiesFactory
.getDefaultMethodProperties("xml"));
   serializer.setWriter(out);

Or including xmlwriter from http://www.generationjava.com/ with:

<dependency>
   <groupId>xmlwriter</groupId>
   <artifactId>xmlwriter</artifactId>
   <version>2.2.2</version>
</dependency>

Or include a ready class form an article in my project to be able to
write the events to a Stream.

Many other projects write their own writer
like Apache JaxMe
http://ws.apache.org/jaxme/apidocs/org/apache/ws/jaxme/XMLWriter.html
or include one from an exiting example in an article or a book.

>From what I see, it's common requirement to serialize sax events, and
write them as XML to a stream.

I don't a big problem inclusing xalan-j or xmlwriter package, but
wondering since this is a common requirement
it should be available with JDK or one of the xml parsers (like
xerces). I did a search but I was not able to find one of these
writers
in JAXP. May be I am doing something wrong here, or missing something
obvious for others, or using these classes in a way that I am not
suppose to.

Any advice ?
Thank you.

---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


Re: Sax events writer

Posted by Michael Ludwig <mi...@gmx.de>.
Michael Glavassevich schrieb am 11.01.2011 um 08:17 (-0500):
> 
> Michael Ludwig <mi...@gmx.de> wrote on 01/11/2011 04:40:50 AM:
> 
> > http://www.ibm.com/developerworks/xml/library/x-tipsxtf/index.html
> >
> > This old article from almost ten years ago talks says you can also
> > invoke the serializer directly, and it still works:
> >
> > import org.apache.xml.serializer.OutputPropertiesFactory;
> > import org.apache.xml.serializer.Serializer;
> > import org.apache.xml.serializer.SerializerFactory;
> >
> > Properties op = OutputPropertiesFactory.getDefaultMethodProperties
> ("xml");
> > Serializer srlzr = SerializerFactory.getSerializer(op);
> > srlzr.setOutputStream(new FileOutputStream("sax2srlzr.xml"));
> > fireEvents(srlzr.asContentHandler()); // calls startDocument() etc
> 
> FYI to folks that this may not be obvious to, this example uses Xalan's
> serializer (serializer.jar), not the one native to Xerces-J
> (org.apache.xml.serialize.* - note the slightly different and confusing
> package name) which was deprecated years ago in favour of the Xalan one.

Ah yes. And to add to the confusion, the old article I'm referring to
above mentions org.apache.xalan.serialize.Serializer (note the different
namespace), yet another serializer namespace, which has been deprecated
as of Xalan 2.7.1, and possibly earlier, and possibly along with the
Xerces-J serializer.

-- 
Michael Ludwig

---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


Re: Sax events writer

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Michael Ludwig <mi...@gmx.de> wrote on 01/11/2011 04:40:50 AM:

...

> http://www.ibm.com/developerworks/xml/library/x-tipsxtf/index.html
>
> This old article from almost ten years ago talks says you can also
> invoke the serializer directly, and it still works:
>
> import org.apache.xml.serializer.OutputPropertiesFactory;
> import org.apache.xml.serializer.Serializer;
> import org.apache.xml.serializer.SerializerFactory;
>
> Properties op = OutputPropertiesFactory.getDefaultMethodProperties
("xml");
> Serializer srlzr = SerializerFactory.getSerializer(op);
> srlzr.setOutputStream(new FileOutputStream("sax2srlzr.xml"));
> fireEvents(srlzr.asContentHandler()); // calls startDocument() etc

FYI to folks that this may not be obvious to, this example uses Xalan's
serializer (serializer.jar), not the one native to Xerces-J
(org.apache.xml.serialize.* - note the slightly different and confusing
package name) which was deprecated years ago in favour of the Xalan one.

> --
> Michael Ludwig
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-users-help@xerces.apache.org

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

Re: Sax events writer

Posted by Michael Ludwig <mi...@gmx.de>.
Michael Glavassevich schrieb am 09.01.2011 um 21:49 (-0500):
> Michael Ludwig <mi...@gmx.de> wrote on 01/09/2011 09:02:36 PM:

> > I think you're looking for org.xml.sax.helpers.DefaultHandler!
> 
> That class doesn't do anything on its own. Sure you can extend it to
> provide implementations of the various SAX2 handler methods, even
> going so far as to write your own SAX serializer, but I think Mansour
> was looking for something which provided that capability out of the
> box.

Thanks! I've never been using a lot, and it shows. :-)

> JAXP does support this. You can use a TransformerHandler [1] to write
> SAX events to a stream:

> SAXTransformerFactory tf = (SAXTransformerFactory)
> TransformerFactory.newInstance();
> TransformerHandler th = tf.newTransformerHandler();
> th.setResult(new StreamResult(...));
> th.startDocument();
> ...
> // fire other SAX events to the TransformerHandler.
> ...
> th.endDocument();

http://www.ibm.com/developerworks/xml/library/x-tipsxtf/index.html

This old article from almost ten years ago talks says you can also
invoke the serializer directly, and it still works:

import org.apache.xml.serializer.OutputPropertiesFactory;
import org.apache.xml.serializer.Serializer;
import org.apache.xml.serializer.SerializerFactory;

Properties op = OutputPropertiesFactory.getDefaultMethodProperties("xml");
Serializer srlzr = SerializerFactory.getSerializer(op);
srlzr.setOutputStream(new FileOutputStream("sax2srlzr.xml"));
fireEvents(srlzr.asContentHandler()); // calls startDocument() etc

-- 
Michael Ludwig

---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


Re: Sax events writer

Posted by Mansour Al Akeel <ma...@gmail.com>.
Michael,
thank you. That helped a lot.

On Sun Jan 09,2011 09:49 pm, Michael Glavassevich wrote:
>    Michael Ludwig <mi...@gmx.de> wrote on 01/09/2011 09:02:36 PM:
>    > Mansour Al Akeel schrieb am 09.01.2011 um 22:30 (+0200):
>    >
>    > > From what I see, it's common requirement to serialize sax events,
>    and
>    > > write them as XML to a stream.
>    > >
>    > > I don't a big problem inclusing xalan-j or xmlwriter package, but
>    > > wondering since this is a common requirement it should be available
>    > > with JDK or one of the xml parsers (like xerces). I did a search
>    but I
>    > > was not able to find one of these writers in JAXP. May be I am
>    doing
>    > > something wrong here, or missing something obvious for others, or
>    > > using these classes in a way that I am not suppose to.
>    >
>    > I think you're looking for org.xml.sax.helpers.DefaultHandler!
>    That class doesn't do anything on its own. Sure you can extend it to
>    provide implementations of the various SAX2 handler methods, even going
>    so far as to write your own SAX serializer, but I think Mansour was
>    looking for something which provided that capability out of the box.
>    JAXP does support this. You can use a TransformerHandler [1] to write
>    SAX events to a stream:
>    ==========================
>    import javax.xml.transform.TransformerFactory;
>    import javax.xml.transform.sax.SAXTransformerFactory;
>    import javax.xml.transform.sax.TransformerHandler;
>    import javax.xml.transform.stream.StreamResult;
>    ...
>    SAXTransformerFactory tf = (SAXTransformerFactory)
>    TransformerFactory.newInstance();
>    TransformerHandler th = tf.newTransformerHandler();
>    th.setResult(new StreamResult(...));
>    th.startDocument();
>    ...
>    // fire other SAX events to the TransformerHandler.
>    ...
>    th.endDocument();
>    ==========================
>    The JAXP transform API is implemented in Xalan and JDK 1.4+.
> 
>    > --
>    > Michael Ludwig
>    >
>    > ---------------------------------------------------------------------
>    > To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
>    > For additional commands, e-mail: j-users-help@xerces.apache.org
>    Thanks.
>    [1]
>    [1]http://xerces.apache.org/xerces2-j/javadocs/api/javax/xml/transform/
>    sax/TransformerHandler.html
>    Michael Glavassevich
>    XML Parser Development
>    IBM Toronto Lab
>    E-mail: mrglavas@ca.ibm.com
>    E-mail: mrglavas@apache.org
> 
> References
> 
>    1. http://xerces.apache.org/xerces2-j/javadocs/api/javax/xml/transform/sax/TransformerHandler.html

---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


Re: Sax events writer

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
Michael Ludwig <mi...@gmx.de> wrote on 01/09/2011 09:02:36 PM:

> Mansour Al Akeel schrieb am 09.01.2011 um 22:30 (+0200):
>
> > From what I see, it's common requirement to serialize sax events, and
> > write them as XML to a stream.
> >
> > I don't a big problem inclusing xalan-j or xmlwriter package, but
> > wondering since this is a common requirement it should be available
> > with JDK or one of the xml parsers (like xerces). I did a search but I
> > was not able to find one of these writers in JAXP. May be I am doing
> > something wrong here, or missing something obvious for others, or
> > using these classes in a way that I am not suppose to.
>
> I think you're looking for org.xml.sax.helpers.DefaultHandler!

That class doesn't do anything on its own. Sure you can extend it to
provide implementations of the various SAX2 handler methods, even going so
far as to write your own SAX serializer, but I think Mansour was looking
for something which provided that capability out of the box. JAXP does
support this. You can use a TransformerHandler [1] to write SAX events to a
stream:

==========================

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;

...

SAXTransformerFactory tf = (SAXTransformerFactory)
TransformerFactory.newInstance();
TransformerHandler th = tf.newTransformerHandler();
th.setResult(new StreamResult(...));
th.startDocument();
...
// fire other SAX events to the TransformerHandler.
...
th.endDocument();

==========================

The JAXP transform API is implemented in Xalan and JDK 1.4+.

> --
> Michael Ludwig
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-users-help@xerces.apache.org

Thanks.

[1]
http://xerces.apache.org/xerces2-j/javadocs/api/javax/xml/transform/sax/TransformerHandler.html

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

Re: Sax events writer

Posted by Michael Ludwig <mi...@gmx.de>.
Mansour Al Akeel schrieb am 09.01.2011 um 22:30 (+0200):

> From what I see, it's common requirement to serialize sax events, and
> write them as XML to a stream.
> 
> I don't a big problem inclusing xalan-j or xmlwriter package, but
> wondering since this is a common requirement it should be available
> with JDK or one of the xml parsers (like xerces). I did a search but I
> was not able to find one of these writers in JAXP. May be I am doing
> something wrong here, or missing something obvious for others, or
> using these classes in a way that I am not suppose to.

I think you're looking for org.xml.sax.helpers.DefaultHandler!

-- 
Michael Ludwig

---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org