Write formatted XML to a file

P

pavel.orehov

Hi,

I create the XML with DOM and save it to a file. But when I open such
file with Notepad, for instance, I see the XML file in one line - it
is not formatted/aligned. If I open it with IExplorer it looks fine.

I am looking for the way to save XML to a file so that XML would be
formatted.

The save code:
-------------------
doc.getDocumentElement().normalize();
DOMSource ds = new DOMSource(doc);
StreamResult sr = new StreamResult(out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
trans.transform(ds, sr);

Thanks,
Pavel
 
E

evgchech

Hi,

I create the XML with DOM and save it to a file. But when I open such
file with Notepad, for instance, I see the XML file in one line - it
is not formatted/aligned. If I open it with IExplorer it looks fine.

I am looking for the way to save XML to a file so that XML would be
formatted.

The save code:
-------------------
doc.getDocumentElement().normalize();
DOMSource ds = new DOMSource(doc);
StreamResult sr = new StreamResult(out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
trans.transform(ds, sr);

Thanks,
Pavel

public static String formatIdent(Document xmlNode) throws
IOException {
StringWriter strWriter = null;
XMLSerializer probeMsgSerializer = null;
OutputFormat outFormat = null;
String identString = null;
try {
probeMsgSerializer = new XMLSerializer();
strWriter = new StringWriter();
outFormat = new OutputFormat();

// Setup format settings
outFormat.setEncoding("UTF-8");
outFormat.setVersion("1.0");
outFormat.setIndenting(true);
// Define a Writer
probeMsgSerializer.setOutputCharStream(strWriter);
// Apply the format settings
probeMsgSerializer.setOutputFormat(outFormat);
// Serialize XML Document
probeMsgSerializer.serialize(xmlNode);

identString = strWriter.toString();
if(identString.indexOf("\n") != -1){
identString =
identString.substring(
identString.indexOf("\n") + 1,
identString.length());
}
strWriter.close();

}
catch (IOException ioEx) {
throw new IOException(
"Failed to format xml document." + ioEx.getMessage());
}
return identString;
}
 
R

RedGrittyBrick

Hi,

I create the XML with DOM and save it to a file. But when I open such
file with Notepad, for instance, I see the XML file in one line - it
is not formatted/aligned. If I open it with IExplorer it looks fine.

I am looking for the way to save XML to a file so that XML would be
formatted.

You are missing three vital steps ...
The save code:

StreamResult sr = new StreamResult(
new OutputStreamWriter(out, "utf-8"));
TransformerFactory tf = TransformerFactory.newInstance();

tf.setAttribute("indent-number", 4);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,260
Messages
2,571,039
Members
48,768
Latest member
first4landlord

Latest Threads

Top