[Date Prev]
| [Thread Prev]
| [Thread Next]
| [Date Next]
--
[Date Index]
| [Thread Index]
Re: [xml-dev] Best way to create an XML document
- From: Michael Glavassevich <mrglavas@ca.ibm.com>
- To: "Mukul Gandhi" <gandhi.mukul@gmail.com>
- Date: Sun, 13 Apr 2008 14:14:25 -0400
"Mukul Gandhi" <gandhi.mukul@gmail.com> 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@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
[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@ca.ibm.com
E-mail: mrglavas@apache.org
[Date Prev]
| [Thread Prev]
| [Thread Next]
| [Date Next]
--
[Date Index]
| [Thread Index]