a problem about xml format for printing

I get a xml Document object,and convert it to a String,print it in the end.

The codes i used is as follows:

DOMSource source = new DOMSource(script);

StringWriter sw = new StringWriter();

StreamResult result = new StreamResult(sw);

try {

TransformerFactory tFactory=TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer(); transformer.transform(source,result);

}catch(TransformerConfigurationException e) {

System.err.println("not possible to create a Transformer instance: " + e);

}catch(TransformerException e2) {

System.err.println("transform err: " + e2);

}

String xmlString = sw.toString();

System.out.println(xmlString);

The out is like this:

<?xml version="1.0" encoding="UTF-8"?><msml version="1.1"><dialogstart><play><audio uri="file://clip1.wav"/></play></dialogstart></msml>

just print every token sequentially.

But i want a hiberarchy struct with indentions

How can i achieve this.

thanks

[1108 byte] By [wxslimea] at [2007-10-2 20:46:09]
# 1

You need to add line sauch as:

TransformerFactory tFactory=TransformerFactory.newInstance();

tFactory.setAttribute("indent-number", new Integer(2)); // new

Transformer transformer = tFactory.newTransformer();

transformer.setOutputProperty(OutputKeys.INDENT,"yes"); // new

transformer.transform(source,result);

Message was edited by:

baostone

baostonea at 2007-7-13 23:29:57 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...