a class to format an XML string into indented xml code

I am looking for a class or a piece of code to format an XML string into indented xml code

for example: an XML string as follows

<servlet><servlet-name>Login</servlet-name>servlet-class>ucs.merch.client.system.LoginServlet</servlet-class></servlet><servlet-mapping><servlet-name>Login</servlet-name>

to format into :

<servlet>

<servlet-name>Login</servlet-name>

<servlet-class>ucs.merch.client.system.LoginServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Login</servlet-name>

<url-pattern>/Login</url-pattern>

</servlet-mapping>

e-mail : pig@jhb.ucs.co.za

[804 byte] By [gomensa] at [2007-9-19]
# 1

Xerces has a class called OutputFormat

If you have your XML document in memory, you can format it using the method setIndenting(true) on the OutputFormat class. The following is an example:

assuming xmlDoc is our document and fileName is the name of the file we wish to write to:

OutputFormat format = new OutputFormat(xmlDoc);

// setup output file name

PrintWriter printwriter = new PrintWriter(new FileWriter(fileName, false));

// construct an XMLSerializer for writing the document

XMLSerializer serializer = new XMLSerializer( printwriter, format );

// Ensure output is indented correctly...

format.setIndenting(true);

// set serializer as a DOM Serializer

serializer.asDOMSerializer();

// serialize the document

serializer.serialize(xmlDoc);

hope this helps!

Rob.

robfoster2000 at 2007-7-5 > top of java,Enterprise & Remote Computing,Enterprise Technologies...