XML formatting not fine in a text editor

Hello Experts,

am generating an xml file through a java program using javax.xml.parsers.

Following is the format of file (say for eg) generated when i see in notepad

<root><tag1>val1</tag1><tag2>val2</tag2></root>

which is not required. I want the file to be in the following format.

<root>

<tag1>val1</tag1>

<tag2>val2</tag2>

</root>

When the file is viewed in any browser, it is fine but is getting messy in a text editor.

Please Help. It would be really appreciated.

Regards

Priya

[634 byte] By [Priya_Indiaa] at [2007-11-27 9:43:00]
# 1

/*

idTransform2.setOutputProperty("indent", "yes");

"indent" property is used for that purpose,use the following program.

*/

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerException;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import org.w3c.dom.Node;

public class SocketExample

{

/**

* @param args

* @throws IOException

* @throws ParserConfigurationException

* @throws TransformerException

*/

public static void main(String[] args) throws IOException, ParserConfigurationException, TransformerException

{

DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();

DocumentBuilder parser = fact.newDocumentBuilder();

Document doc = parser.newDocument();

Node root = doc.createElement("request");

doc.appendChild(root);

Node Last_Name_Node = doc.createElement("Last_Name");

root.appendChild(Last_Name_Node);

Last_Name_Node.appendChild(doc.createTextNode("Last_Name"));

Node Title_Node = doc.createElement("Title");

root.appendChild(Title_Node);

Title_Node.appendChild(doc.createTextNode("Title"));

//setting Servcie Address1

Node Service_Address1_Node = doc.createElement("Service_Address1");

root.appendChild( Service_Address1_Node);

Service_Address1_Node.appendChild(doc.createTextNode("Service_Address1"));

TransformerFactory xformFactory2 = TransformerFactory.newInstance();

Transformer idTransform2 = xformFactory2.newTransformer();

javax.xml.transform.Source input2 = new DOMSource(doc);

javax.xml.transform.Result output2 = new StreamResult(new java.io.FileWriter("d:/webapp.log",true));

idTransform2.setOutputProperty("indent", "yes");

idTransform2.transform(input2, output2);

}

}

Message was edited by:

BhavaniRajan

BhavaniRajana at 2007-7-12 23:47:31 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...
# 2
Thankyou Soo much :-) the solution has worked !Best Regards,Priya
Priya_Indiaa at 2007-7-12 23:47:31 > top of Java-index,Enterprise & Remote Computing,Enterprise Technologies...