You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Sampath K Settipalli <Sa...@genome.wi.mit.edu> on 2002/09/30 16:38:34 UTC
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: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org