Upon further reading of your email, I realized the code I supplied does not
do what you desired.  The functionality you seek lies in the
'setPreserveSpace(true)' method of OutputFormat.  You also need to place
your newline characters in more selective locations.  I believe the
following code is what you were really looking for.

Enjoy!

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

      Element sampleEl = doc.createElement("SampleElement");
      // add these lines //
      doc.appendChild(sampleEl);
      Element docElement = doc.getDocumentElement();
      docElement.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);
        // add this line //
        docElement.appendChild(doc.createTextNode("\n"));
//        sampleEl.appendChild(doc.createTextNode("\n"));
      }

      File file = new File("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.setPreserveSpace(true);

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

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