You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by "Fiddler, Peter A" <pe...@boeing.com> on 2002/01/25 00:08:03 UTC
Indentation in output XML
Dear Xerces,
I am using jaxp-1.1 to construct a DOM, then translate it into a String (and
eventually into a file). I set indent to "yes" in the Transformer
(Transformer.setOutputProperty(OutputKeys.INDENT, "yes")), but the output
XML is still not indented. What am I missing, or is this feature just not
implemented in jaxp-1.1?
Here is problem code:
protected String getDocument(String dtdFileName) throws SMITSException
{
// Initialize our return value
String xmlString = null;
try
{
// Create Transformer to translate source tree to output result
TransformerFactory tF = TransformerFactory.newInstance();
Transformer tr = tF.newTransformer();
tr.setOutputProperty(OutputKeys.INDENT, "yes");
tr.setOutputProperty(OutputKeys.METHOD, "xml");
tr.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dtdFileName);
// Create a DOM Source, a holder for a transformation Source tree
// in the form of a Document Object Model (DOM) tree, passing in
// our XML Document, which is an org.w3c.dom.Node.
DOMSource domSrc = new DOMSource(m_xmlDocument);
// Create a StreamResult, which is a holder for a transformation
// result (which may be XML or some other form of markup),
// initializing the byte stream that is to be written to.
ByteArrayOutputStream output = new ByteArrayOutputStream();
StreamResult result = new StreamResult(output);
// Transform the source tree to the output result
tr.transform(domSrc, result);
// Flush the output stream and force any buffered output bytes
// to be written out. The general contract of flush() is that
// calling it is an indication that, if any bytes previously
// written have been buffered by the implementation of the output
// stream, such bytes should immediately be written to their
// intended destination.
output.flush();
// Convert the output stream to a byte array
byte[] byteArray = output.toByteArray();
// Construct a byte array input stream using the byte array
ByteArrayInputStream input = new ByteArrayInputStream(byteArray);
// Initialize a SAXParser
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setValidating(true);
SAXParser sp = spf.newSAXParser();
SMITSHandler sh = new SMITSHandler();
// Let the SAXParser validate the document; throws
SAXParseException if invalid
sp.parse(input, sh);
// Close the input stream (once it is read, it is no longer
available anyway)
input.close();
// If we are here we made it through the parse process. We get the
string
// from the output of the transformation.
xmlString = output.toString();
// Close the output stream to free up resources
output.close();
}
... many catch blocks omitted ...
return xmlString;
}
Pete Fiddler
206-544-3325
peter.a.fiddler@boeing.com
Re: Indentation in output XML
Posted by Arturo Ma <am...@fgm.com>.
Peter:
If you are using Apache's Xalan XSLT Processor to perform your
transformation then you can
use Apache's Xalan indentation feature.
You can add the following to your code:
// This property is specific to Apache Xalan XSLT processor.
public static final String XALAN_INDENT_AMOUNT =
"{http://xml.apache.org/xslt}indent-amount";
// The value of "2" here means two spaces.
tr.setOutputProperty((XALAN_INDENT_AMOUNT, "2");
Hope this helps.
Remember, this is a XALAN feature ONLY.
Arturo.
-------------------------------------------------------------
Arturo Ma
ama@fgm.com
FGM Inc.
2820 Camino Del Rio South, Suite 130
San Diego, CA 92108
Office: 619. 297.2905
Fax: 619. 297.2923
http://www.fgm.com
----- Original Message -----
From: "Fiddler, Peter A" <pe...@boeing.com>
To: <xe...@xml.apache.org>
Sent: Thursday, January 24, 2002 3:08 PM
Subject: Indentation in output XML
> Dear Xerces,
>
> I am using jaxp-1.1 to construct a DOM, then translate it into a String
(and
> eventually into a file). I set indent to "yes" in the Transformer
> (Transformer.setOutputProperty(OutputKeys.INDENT, "yes")), but the output
> XML is still not indented. What am I missing, or is this feature just not
> implemented in jaxp-1.1?
>
> Here is problem code:
>
> protected String getDocument(String dtdFileName) throws SMITSException
> {
> // Initialize our return value
> String xmlString = null;
>
> try
> {
> // Create Transformer to translate source tree to output result
> TransformerFactory tF = TransformerFactory.newInstance();
> Transformer tr = tF.newTransformer();
> tr.setOutputProperty(OutputKeys.INDENT, "yes");
> tr.setOutputProperty(OutputKeys.METHOD, "xml");
> tr.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, dtdFileName);
>
> // Create a DOM Source, a holder for a transformation Source tree
> // in the form of a Document Object Model (DOM) tree, passing in
> // our XML Document, which is an org.w3c.dom.Node.
> DOMSource domSrc = new DOMSource(m_xmlDocument);
>
> // Create a StreamResult, which is a holder for a transformation
> // result (which may be XML or some other form of markup),
> // initializing the byte stream that is to be written to.
> ByteArrayOutputStream output = new ByteArrayOutputStream();
> StreamResult result = new StreamResult(output);
>
> // Transform the source tree to the output result
> tr.transform(domSrc, result);
>
> // Flush the output stream and force any buffered output bytes
> // to be written out. The general contract of flush() is that
> // calling it is an indication that, if any bytes previously
> // written have been buffered by the implementation of the output
> // stream, such bytes should immediately be written to their
> // intended destination.
> output.flush();
>
> // Convert the output stream to a byte array
> byte[] byteArray = output.toByteArray();
>
> // Construct a byte array input stream using the byte array
> ByteArrayInputStream input = new ByteArrayInputStream(byteArray);
>
> // Initialize a SAXParser
> SAXParserFactory spf = SAXParserFactory.newInstance();
> spf.setValidating(true);
> SAXParser sp = spf.newSAXParser();
> SMITSHandler sh = new SMITSHandler();
>
> // Let the SAXParser validate the document; throws
> SAXParseException if invalid
> sp.parse(input, sh);
>
> // Close the input stream (once it is read, it is no longer
> available anyway)
> input.close();
>
> // If we are here we made it through the parse process. We get
the
> string
> // from the output of the transformation.
> xmlString = output.toString();
>
> // Close the output stream to free up resources
> output.close();
> }
>
> ... many catch blocks omitted ...
>
> return xmlString;
> }
>
> Pete Fiddler
> 206-544-3325
> peter.a.fiddler@boeing.com
>
----------------------------------------------------------------------------
----
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-user-help@xml.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org