Format XML when writing to a file

P

pavel.orehov

Hi,

I am saving created XML to a file but when I open the file with
nodepad, for instance, I see all the XML in one line.
It is not well formatted/alignment.
Only when I open it with IExplorer I see it okey.

This is the code of saving:
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);

Does anyone knows how should I save the XML to file so that the XML
file will be well formatted ?

Thanks,
Pavel
 
R

rossum

Hi,

I am saving created XML to a file but when I open the file with
nodepad, for instance, I see all the XML in one line.
It is not well formatted/alignment.
Only when I open it with IExplorer I see it okey.

This is the code of saving:
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);

Does anyone knows how should I save the XML to file so that the XML
file will be well formatted ?

Thanks,
Pavel
This is probably due to Notepad and Java using different end-of-line
characters. Try using a different text editor to see if it can
interpret the EoLs correctly.

rossum
 
E

evgchech

This is probably due to Notepad and Java using different end-of-line
characters. Try using a different text editor to see if it can
interpret the EoLs correctly.

rossum

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

Real Gagnon

Hi,

I am saving created XML to a file but when I open the file with
nodepad, for instance, I see all the XML in one line.
It is not well formatted/alignment.
Only when I open it with IExplorer I see it okey.

This is the code of saving:
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);

Does anyone knows how should I save the XML to file so that the XML
file will be well formatted ?

try this

TransformerFactory tf = TransformerFactory.newInstance();
Transformer trans = tf.newTransformer();
//
trans.setOutputProperty(OutputKeys.METHOD, "xml");
trans.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
trans.setOutputProperty
("{http://xml.apache.org/xslt}indent-amount", "4");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
trans.transform(domSource, sr);
//

Bye.
 

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
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top