[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Best way to create an XML document
"Mukul Gandhi" <gandhi.mukul@g...> wrote on 04/13/2008 12:59:56 PM: > On Sat, Apr 12, 2008 at 2:19 PM, Andrew Welch <andrew.j.welch@gmail. > com> wrote: > > Alternatively use the streaming api in Java 6 - see XMLStreamWriter. > > I tried using XMLStreamWriter (I guess this is a StAX interface). > > As for the well-formedness requirement, I think this is better than > the SAX calls (the program I posted earlier in this thread). > > Please consider this example: > > XMLOutputFactory factory = XMLOutputFactory.newInstance(); > XMLStreamWriter writer = factory.createXMLStreamWriter(System.out); > writer.writeStartDocument(); > writer.writeStartElement("x"); > writer.writeStartElement("y"); > writer.writeAttribute("attr1", "123"); > writer.writeEndDocument(); > writer.flush(); > writer.close(); > > This produces the output (as intended by me): > > <?xml version="1.0" ?><x><y attr1="123"></y></x> > > I think it's hard to create ill-formed XML output with > XMLStreamWriter. Or, can we? It keeps the tags nested properly but you can still write all sorts of garbage. Extending your example (spot the errors): writer.writeStartDocument(); writer.writeDTD("<!GARBAGE>"); writer.writeComment("bad -- comment"); writer.writeStartElement("x"); writer.writeCharacters("\u0000"); writer.writeProcessingInstruction("xml", "version=\"1.0\""); writer.writeStartElement("y"); writer.writeAttribute("attr1", "123"); writer.writeAttribute("attr1", "456"); writer.writeEndElement(); writer.writeEndElement(); writer.writeEmptyElement("3"); writer.writeEndDocument(); writer.flush(); writer.close(); produces: <?xml version="1.0" ?><!GARBAGE><!--bad -- comment--><x><?xml version="1.0"?><y attr1="123" attr1="456"></y></x><3/> with a NUL char after <x>. > But the Java docs say - "The XMLStreamWriter does not perform well > formedness checking on its input". What does this mean? It means what it says. It doesn't check that the document you're writing is well-formed. > Can we pretty print the XML output with StaX? Depends on the implementation. There's no standard property in the StAX spec for that. > Can we use StaX with JDK 1.4, in a stable manner? I need to run this > on a PC where I cannot upgrade to JDK 1.6.x. Sure if you can find an implementation which will run on JDK 1.4. Woodstox [1] maybe? Xerces [2] eventually. There's no such limitation in the API. Last time I checked none of the signatures in javax.xml.stream required more than JDK 1.2. > -- > Regards, > Mukul Gandhi > > _______________________________________________________________________ > > 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@l... > subscribe: xml-dev-subscribe@l... > List archive: http://lists.xml.org/archives/xml-dev/ > List Guidelines: http://www.oasis-open.org/maillists/guidelines.php [1] http://woodstox.codehaus.org/ [2] http://svn.apache.org/viewvc/xerces/java/branches/stax-dev/src/org/apache/xerces/stax/ Michael Glavassevich XML Parser Development IBM Toronto Lab E-mail: mrglavas@c... E-mail: mrglavas@a...
[Date Prev] | [Thread Prev] | [Thread Next] | [Date Next] -- [Date Index] | [Thread Index] |
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|