I see something similar. I use the snippet of code below to serialize an XML
document that I have created in memory:
this.serializer = new XMLSerializer();
this.serializer.setOutputCharStream( this.writer );
// Initialise the OutputFormat
this.format = new OutputFormat();
this.format.setOmitXMLDeclaration( false );
this.format.setEncoding( encoding );
this.format.setIndenting( true );
this.format.setLineSeparator( System.getProperty( "line.separator") );
// Tell the serializer the OutputFormat to use
this.serializer.setOutputFormat( format );
this.parser = new SAXParser();
// Wrap the parser with a XMLReaderAdapter so that we can call
// startElement() and endElement() methods.
this.readerAdapter = new XMLReaderAdapter( parser );
// Tell the parser the ContentHander to use
this.parser.setContentHandler( serializer );
this.parser.startDocument();
The output looks perfect!! Evertyhing is indented properly depending on how
deep the element is.
However, it does not seem to work if I use the same snippet for XML
documents that I read from disk. I usually get some XML documents that were
not "visually" perfect to look at. So I wrote the code below, taking ideas
from the above. But it does not work. I tried Method.XML, Method.TXT, even
Method.HTML.
// Open file for reading, with encoding specified
InputStreamReader isr = new InputStreamReader( new FileInputStream(
xmlFile ), encoding );
this.reader = new BufferedReader( isr );
// Same thing with output. Encoding must be specified
OutputStreamWriter out = new OutputStreamWriter( new FileOutputStream(
outputXmlFile ), encoding );
this.writer = out;
// Serializer will serialize to output file
this.serializer = new XMLSerializer();
this.serializer.setOutputCharStream( this.writer );
// Initialise the OutputFormat
this.format = new OutputFormat( Method.XML, encoding, true );
this.format.setOmitXMLDeclaration( false );
this.format.setPreserveEmptyAttributes( false );
this.format.setPreserveSpace( false );
this.format.setLineSeparator( "\r\n" );
// Tell the serializer the OutputFormat to use
this.serializer.setOutputFormat( format );
// Create a parser
this.parser = new SAXParser();
this.readerAdapter = new XMLReaderAdapter( parser );
// Tell the parser the ContentHander to use
// which will then serialize to output file
this.parser.setContentHandler( serializer );
// Parse
this.parser.parse( new InputSource( this.reader ));
this.reader.close();
this.writer.flush();
this.writer.close();
John
----- Original Message -----
From: "Ed Jenkins" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Friday, February 22, 2002 2:23 PM
Subject: Re: can XMLSerializer create nice formatted output ?
> I have yet to find any program that will format XML perfectly. The best
> thing I have found is to output with no whitespace (so you get one line
that
> is 10 miles long). Then, when necessary, copy and paste it into XML Spy,
> then have it pretty-print that. Even that is not perfect, but it's the
best
> I've seen yet.
>
> Ed
>
> ---
>
> From: "Hellmann Peter (ext) ICM RDC IS VDR RD"
> <[EMAIL PROTECTED]>
> Reply-To: [EMAIL PROTECTED]
> To: "'[EMAIL PROTECTED]'" <[EMAIL PROTECTED]>
> Subject: can XMLSerializer create nice formatted output ?
> Date: Wed, 20 Feb 2002 18:37:56 +0100
>
> hi all,
>
> i am using the XMLSerializer with an OutputFormat object. I do serialize a
> xml document that has
> been previously loaded using DOMParser. After altering the xml document,
the
> output, the serializer
> creates is formatted in a wierd way. Tags that have been deleted show an
> empty line in the output
> while altered or newly created tags do not line break after the closing
tag.
> Is there a way to
> create a nice formatted output? Thanks for any hints.
>
> kind regards
> Peter Brightman
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
> _________________________________________________________________
> Join the worlds largest e-mail service with MSN Hotmail.
> http://www.hotmail.com
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]