Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SAX Parser for creating (not parsing) XML document

0 views
Skip to first unread message

Naresh Agarwal

unread,
Jul 21, 2008, 2:52:13 AM7/21/08
to
Hi

I have been using DOM parser to create XML documents. I want to use an
alternate mechanism to create XML document as size of my XML document
is large and I don;t want the overhead of DOM (where entire tree in
constructed in memory).

Like SAX APIs for parsing XML documents, Is there any thing like SAX
parser/APIs for creating XML documents?

Is there any other standard mechanism of creating XML document,
without requiring the entire tree structure to be constructed in
memory?

Thanks,
Naresh

Martin Honnen

unread,
Jul 21, 2008, 7:09:13 AM7/21/08
to
Naresh Agarwal wrote:
> Hi
>
> I have been using DOM parser to create XML documents. I want to use an
> alternate mechanism to create XML document as size of my XML document
> is large and I don;t want the overhead of DOM (where entire tree in
> constructed in memory).
>
> Like SAX APIs for parsing XML documents, Is there any thing like SAX
> parser/APIs for creating XML documents?

Java 6 has XMLStreamWriter
http://www.java2s.com/Tutorial/Java/0440__XML/UsingXMLStreamWritertocreateXMLfile.htm


--

Martin Honnen
http://JavaScript.FAQTs.com/

Message has been deleted

Naresh Agarwal

unread,
Jul 22, 2008, 12:46:42 AM7/22/08
to
On Jul 21, 4:09 pm, Martin Honnen <mahotr...@yahoo.de> wrote:
> Naresh Agarwal wrote:
> > Hi
>
> > I have been using DOM parser to create XML documents. I want to use an
> > alternate mechanism to create XML document as size of my XML document
> > is large and I don;t want the overhead of DOM (where entire tree in
> > constructed in memory).
>
> > Like SAX APIs for parsing XML documents, Is there any thing like SAX
> > parser/APIs for creating XML documents?
>
> Java 6 has XMLStreamWriterhttp://www.java2s.com/Tutorial/Java/0440__XML/UsingXMLStreamWritertoc...

>
> --
>
> Martin Honnen
> http://JavaScript.FAQTs.com/

Thanks. Is there any thing available for this with Java 1.5, any open
source app?

Best
Naresh

Stanimir Stamenkov

unread,
Jul 22, 2008, 1:02:49 AM7/22/08
to
Mon, 21 Jul 2008 21:46:42 -0700 (PDT), /Naresh Agarwal/:

> On Jul 21, 4:09 pm, Martin Honnen <mahotr...@yahoo.de> wrote:
>
>> Java 6 has XMLStreamWriterhttp://www.java2s.com/Tutorial/Java/0440__XML/UsingXMLStreamWritertoc...
>
> Thanks. Is there any thing available for this with Java 1.5, any open
> source app?

<http://en.wikipedia.org/wiki/StAX> lists three implementations:

> * http://stax.codehaus.org/ Reference Implementation
> * Woodstox Open source StAX implementation
> * https://sjsxp.dev.java.net is Sun's Stax implementation

--
Stanimir

Tony Lavinio

unread,
Jul 22, 2008, 9:16:29 AM7/22/08
to
Stefan Ram wrote:
> Martin Honnen <maho...@yahoo.de> writes:
>> Java 6 has XMLStreamWriter
>
> Often, people use »tag« for »element«.
>
> Here, it seems to be the other way round:
>
> xtw.writeStartElement("http://www.w3.org/TR/REC-html40", "a");
> xtw.writeAttribute("href", "http://www.java2s.com");
> xtw.writeCharacters("here");
> xtw.writeEndElement();
>
> . It seems as »writeEndElement« is supposed
> to write the end /tag/, i.e., "</a>".
>
> So, more correct names to me would be:
>
> »writeEndTag()«,
> »writeEndOfElement()«, or
> »writeElementEnding()«.

You're not writing tags with this, you're writing /events/.

So writeStartElement means you are sending the StartElement
/event/ into the stream. writeEndElement means you are
sending the EndElement /event/ into the stream.

When using XMLStreamWriter, you must remember that you are
acting as the parser.


--
Tony Lavinio <> DataDirect <> Stylus Studio XML <> alav...@progress.com
XQuery, XSLT, XML Schema and EDI Toolset <> http://www.stylusstudio.com/
<> There is no problem that brute force and ignorance cannot overcome <>

Message has been deleted

Stanimir Stamenkov

unread,
Jul 23, 2008, 1:04:44 AM7/23/08
to
22 Jul 2008 16:31:54 GMT, /Stefan Ram/:

> Another way to write XML with pre-1.6-Java:
>
> com.sun.org.apache.xml.internal.serialize.XMLSerializer serializer =
> new com.sun.org.apache.xml.internal.serialize.XMLSerializer
> ( fileOutputStream, outputFormat);
> serializer.startDocument();
> { serializer.startElement("", "example", "example",
> new com.sun.org.apache.xml.internal.serializer.
> AttributesImplSerializer() );
> { final char[] data = "test text" . toCharArray();
> serializer.characters( data, 0, data.length );
> serializer.endElement( "", "example", "example" ); }
> serializer.endDocument(); }}}

Here's more standard way of doing it without requiring
implementation specific classes (works with Java 1.4):

http://mail-archives.apache.org/mod_mbox/xml-xalan-j-users/200801.mbox/%3c4797292...@netscape.net%3e

--
Stanimir

Philippe Poulard

unread,
Jul 24, 2008, 4:52:15 AM7/24/08
to
Stefan Ram a écrit :

> Naresh Agarwal <naresh....@gmail.com> writes:
>> Is there any thing available for this with Java 1.5, any open
>> source app?
>
> Writing XML is simpler than reading XML.
>
> So, sometimes, one can get by with no library at all:
>
> outStream.print( "<" );
> outStream.print( type );
> outStream.print( ">" );
>

hi,

IMHO, this is one of the worst practice

outStream.print( "<" );
outStream.print( type );
outStream.print( ">" );
outStream.print( "if (a<b ) {}" );

you'll have too many occasions to break your XML result ; rely on tools
made for that purpose that will produce well-formed XML

you can also use templates for creating SAX documents with RefleX :
http://reflex.gforge.inria.fr/tips.html#createFromScratch
of course, you can loop on whatever you want to create content (merge
thousand XML files for example:
http://reflex.gforge.inria.fr/tips.html#parsingFragments )

then pipe the created doc to an XSLT serializer :
http://reflex.gforge.inria.fr/tips.html#xsltSerialization

you can use it from the command line, embed it in a program, or deploy
it within a web server

tutorials are available here :
http://reflex.gforge.inria.fr/tutorial.html

I will talk about all that stuff at Balisage 2008 in Montreal, with a
focus on schema languages
http://www.balisage.net/Program.html#h245p

--
Cordialement,

///
(. .)
--------ooO--(_)--Ooo--------
| Philippe Poulard |
-----------------------------
http://reflex.gforge.inria.fr/
Have the RefleX !

0 new messages