Make the following changes and your code will be properly indented:

------------------
 DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
 DocumentBuilder db = df.newDocumentBuilder();
 Document doc = db.newDocument();

 Element sampleEl = doc.createElement("SampleElement");
// sampleEl.appendChild(doc.createTextNode("\n"));
 for (int i=0; i < 10; i++) {
     Element testEl = doc.createElement("TestEl");
     testEl.setAttribute("r_name", "rname" + Integer.toString(i));
     testEl.setAttribute("atestattr", "atestattribute" +
Integer.toString(i));

     Element prdEl = doc.createElement("Template");
     prdEl.setAttribute("prname", "prname" + Integer.toString(i));
     testEl.appendChild(prdEl);
//     testEl.appendChild(doc.createTextNode("\n"));

//     sampleEl.appendChild(doc.createTextNode("\n"));
     sampleEl.appendChild(testEl);
//     sampleEl.appendChild(doc.createTextNode("\n"));
       }

 File file = new File("/path", "test.data");
 PrintWriter pw = new PrintWriter(new FileOutputStream(file));

 OutputFormat outputFormat = new OutputFormat(doc);
 outputFormat.setOmitComments(true);
 outputFormat.setOmitDocumentType(true);
 outputFormat.setOmitXMLDeclaration(true);
 outputFormat.setLineSeparator("\n");
 // add this line //
 outputFormat.setIndenting(true);

XMLSerializer serializer = new XMLSerializer(pw,outputFormat);
 serializer.asDOMSerializer();
 serializer.serialize(sampleEl);
-----------------------------------

As for the attributes.  Order is not guaranteed in XML and therefore you
cannot force an order (unless you do it yourself), nor should you be relying
on any specific ordering of the attributes.

Brion Swanson

-----Original Message-----
From: Sampath K Settipalli [mailto:[EMAIL PROTECTED]
Sent: Monday, September 30, 2002 10:39 AM
To: [EMAIL PROTECTED]
Subject: doesn't create a new line while serializing


Hi ,

     When I try to serialize a Document/Element it doesn't create  a new
line after each Element. All the Elements are in 1 line.  I did set the
outputformat. Element.appendChild(doc.createTextNode("\n"));  Shouldn't
this create a new line ?? Below is my test code. Can some suggest what
am I missing. The other thing is when it writes to a  file it sorts the
Attributes. How can I set it not to sort attributes and write in the
same order as set.

-------------
 DocumentBuilderFactory df = DocumentBuilderFactory.newInstance();
 DocumentBuilder db = df.newDocumentBuilder();
 Document doc = db.newDocument();

 Element sampleEl = doc.createElement("SampleElement");
 sampleEl.appendChild(doc.createTextNode("\n"));
 for (int i=0; i < 10; i++) {
     Element testEl = doc.createElement("TestEl");
     testEl.setAttribute("r_name", "rname" + Integer.toString(i));
     testEl.setAttribute("atestattr", "atestattribute" +
Integer.toString(i));

     Element prdEl = doc.createElement("Template");
     prdEl.setAttribute("prname", "prname" + Integer.toString(i));
     testEl.appendChild(prdEl);
     testEl.appendChild(doc.createTextNode("\n"));

     sampleEl.appendChild(doc.createTextNode("\n"));
     sampleEl.appendChild(testEl);
     sampleEl.appendChild(doc.createTextNode("\n"));
       }

 File file = new File("/path", "test.data");
 PrintWriter pw = new PrintWriter(new FileOutputStream(file));

 OutputFormat outputFormat = new OutputFormat(doc);
 outputFormat.setOmitComments(true);
 outputFormat.setOmitDocumentType(true);
 outputFormat.setOmitXMLDeclaration(true);
 outputFormat.setLineSeparator("\n");

XMLSerializer serializer = new XMLSerializer(pw,outputFormat);
 serializer.asDOMSerializer();
 serializer.serialize(sampleEl);
-----------------------------------

 I tried setting outputFormat.setIndenting(true); For each element it
indents to more than one line which I definitely don't want. I want each
testEl Element as one line.

thanks in Advance,
Sampath


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

<<application/ms-tnef>>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to