Hi Ralf,

Here is some code that I use to set indentation. Note that I am no Xalan
guru; this works for me but may not be the "correct" solution.

OutputProperties outputFormat = new OutputProperties();
outputFormat.setProperty(OutputKeys.METHOD, Method.XML);
outputFormat.setProperty(OutputKeys.INDENT, "yes");
outputFormat.setProperty(OutputProperties.S_KEY_INDENT_AMOUNT, "7");
Serializer serialiser =
SerializerFactory.getSerializer(outputFormat.getProperties());

Note that if the original document has whitespace nodes, then the
indenting
won't occur (ie whitespace in the document overrides indenting). Try
putting
  <xsl:strip-space elements="*"/>
at the top-level of your stylesheet to see if that has the desired
effect with
your current code...

Note also that setProperty with key of METHOD implicitly calls 
getDefaultMethodProperties, so this code isn't as different from
yours as it appears. Which is the "right" approach I am not sure :-)

Hope this helps,

Simon

> -----Original Message-----
> From: Ralf Steppacher [mailto:[EMAIL PROTECTED] 
> Sent: Tuesday, July 09, 2002 7:57 PM
> To: [EMAIL PROTECTED]
> Subject: Serializer does not indent
> 
> 
> Hello everybody.
> 
> I tried to use the the org.apache.xalan.serialize Packge to 
> serialize a 
> DOM to System.out. My problem is, that I can not get 
> indention to work. 
> More precise, indention is always one space per level, 
> regardles of the 
> values of 'indent' and '{http://xml.apache.org/xslt}indent-amount' of 
> the output properties.
> 
> Could anybody please point out to me what I am missing?
> My environment is Xalan 2.3.1, Xerces 2.0.1, sdk1.2, Win2k.
> 
> Thanks in advance!
> Ralf
> 
> 
> ---- code snippet ----
> 
> DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
> dbf.setNamespaceAware( true );
> dbf.setValidating( false );
> DocumentBuilder db = null;
> try { db = dbf.newDocumentBuilder(); }
> catch ( ParserConfigurationException e ) {
>      e.printStackTrace();
>      System.exit( 1 );
> }
> 
> File xmlFile = new File( "D:\\Benutzer\\Ralf\\allinone_1.xml" 
> ); if ( !xmlFile.canRead() ) System.exit( 1 );
> 
> Document doc = null;
> try { doc = db.parse( xmlFile ); }
> catch ( SAXException e ) { e.printStackTrace(); System.exit( 
> 2 ); } catch ( IOException e ) { e.printStackTrace(); 
> System.exit( 3 ); }
> 
> Properties outProps =
>      OutputProperties.getDefaultMethodProperties("xml");
> outProps.setProperty( "indent", "yes" );
> outProps.setProperty( 
> "{http://xml.apache.org/xslt}indent-amount";, > "4" ); 
> Serializer serializer = SerializerFactory.getSerializer( 
> outProps ); serializer.setOutputStream( System.out ); try {
>      DOMSerializer ds = serializer.asDOMSerializer();
>      /* The following prints auf "4" as one expects. */
>      System.out.println( ((SerializerToXML)ds).m_indentAmount );
>      doc.normalize();
>      ds.serialize( doc );
> }
> catch ( IOException e ) { e.printStackTrace(); System.exit( 4 ); }
> 
> ----
> 
> 

Reply via email to