XML.orgXML.org
FOCUS AREAS |XML-DEV |XML.org DAILY NEWSLINK |REGISTRY |RESOURCES |ABOUT
OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.

 


Help: OASIS Mailing Lists Help | MarkMail Help

[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]
Re: [xml-dev] com.megginson.sax.XMLWriter

I do exactly the same when I need to write XML from Java. But I prefer a lot using XSL to do this, when I can...

Best,

Christophe


Le 20/09/2016 à 22:53, Michael Kay a écrit :
If you're prepared to make the class that originates SAX events implement XMLReader, then it's easily done using a JAXP identity transformation from a SAXSource to a StreamResult.

If you don't want to do that, and just want to feed events into a ContentHandler, then you can set up a pipeline in Saxon without too much difficulty:

Processor proc = new Processor();
Serializer ser = proc.newSerializer(writer);
ser.setOutputProperty(....);
ContentHandler ch = ser.getContentHandler();

and then write to the ContentHandler ch.

Personally though if I'm writing XML from Java I much prefer the Stax XMLStreamWriter interface to the SAX ContentHandler. (People often forget that StAX has push APIs as well as pull). Getting an XMLStreamWriter from Saxon is just as easy:

Processor proc = new Processor();
Serializer ser = proc.newSerializer(writer);
ser.setOutputProperty(....);
XMLStreamWriter xsw = ser.getXMLStreamWriter();

I usually then front-end this with a custom class that knows about namespaces and possibly uses a chaining pattern, so the application reduces to something like

out.startElement("e").att("id", "23").att("name", "fred").text("content").endElement();
out.startElement("f").att("id", "29").att("name", "bill").text("content").endElement();

etc.

Michael Kay
Saxonica


On 20 Sep 2016, at 21:04, Elliotte Rusty Harold <elharo@ibiblio.org> wrote:

Is there a more modern, supported alternative to
com.megginson.sax.XMLWriter: http://www.megginson.com/downloads/

That is, a SAX filter to write an XML document from a SAX event stream
onto a Java Writer.

--
Elliotte Rusty Harold
elharo@ibiblio.org

_______________________________________________________________________

XML-DEV is a publicly archived, unmoderated list hosted by OASIS
to support XML implementation and development. To minimize
spam in the archives, you must subscribe before posting.

[Un]Subscribe/change address: http://www.oasis-open.org/mlmanage/
Or unsubscribe: xml-dev-unsubscribe@lists.xml.org
subscribe: xml-dev-subscribe@lists.xml.org
List archive: http://lists.xml.org/archives/xml-dev/
List Guidelines: http://www.oasis-open.org/maillists/guidelines.php

_______________________________________________________________________

XML-DEV is a publicly archived, unmoderated list hosted by OASIS
to support XML implementation and development. To minimize
spam in the archives, you must subscribe before posting.

[Un]Subscribe/change address: http://www.oasis-open.org/mlmanage/
Or unsubscribe: xml-dev-unsubscribe@lists.xml.org
subscribe: xml-dev-subscribe@lists.xml.org
List archive: http://lists.xml.org/archives/xml-dev/
List Guidelines: http://www.oasis-open.org/maillists/guidelines.php








[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index]


News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 1993-2007 XML.org. This site is hosted by OASIS