[jdom-interest] Line feeds added after re-writing of XML file
Aaron
trotfox at writeme.com
Tue Jul 10 18:44:06 PDT 2001
Hi, I am a newbie to java, I am writing a JSP file that reads an XML file
then adds an Element as a child of the root, everything works okay, but
there is one "Feature" I don't like much which is the addition of several
line feeds just after the root and before the list of elements. This is not
a big problem, just a mere anoyance, but I can see some problems in the
future, with it.
Is it my lake of skill or is there a feature that I don't know about?
Thanks
Aaron
=========================
Examples - The XML : before
<dtd and doctype stuff>
<root>
<element></element>
<element></element>
<element></element>
<element></element>
</root>
Examples - The XML : After
<root>
<newelement></newelement>
<element></element>
<element></element>
<element></element>
<element></element>
</root>
Examples - The JSP :
<%@ page import="org.jdom.*"%>
<%@ page import="org.jdom.Element.*"%>
<%@ page import="org.jdom.output.*"%>
<%@ page import="org.jdom.input.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%
String entryTitle = "titlex";
String entryTime = "timex";
String entryDay = "dayx";
String entryMonth = "monthx";
String entryYear = "entryx";
String entryLocation = "locationx";
String entryContent = "content";
SAXBuilder myBuilder = new SAXBuilder(true);
Document diary = myBuilder.build(new
File(getServletContext().getRealPath("/bro.sweet.as/diary/diary.xml")));
Element diaryElement = diary.getRootElement();
List diaryContents;
Iterator diaryContentsIterator;
diaryContents = diary.getRootElement().getChildren();
file://System.out.println(diaryContents.size());
file://add new
Element entryElement = new Element("entry");
entryElement.setAttribute(new Attribute("title",entryTitle));
entryElement.setAttribute(new Attribute("time",entryTime));
entryElement.setAttribute(new Attribute("day",entryDay));
entryElement.setAttribute(new Attribute("month",entryMonth));
entryElement.setAttribute(new Attribute("year",entryYear));
entryElement.setAttribute(new Attribute("location",entryLocation));
entryElement.addContent(entryContent);
diaryContents.add(0, entryElement);
diaryContentsIterator = diaryContents.iterator();
while(diaryContentsIterator.hasNext()){
Element diaryEntry = (Element) diaryContentsIterator.next();
diaryElement.addContent(diaryEntry.detach());
diaryElement.removeContent(new CDATA("\n"));
}
file://write file
try {
XMLOutputter outputter = new XMLOutputter("", true);
outputter.output(diary, System.out);
FileWriter writer = new
FileWriter(getServletContext().getRealPath("/bro.sweet.as/diary/diary.xml"))
;
outputter.output(diary, writer);
writer.close();
} catch (java.io.IOException e) {
e.printStackTrace();
}
System.out.println(diaryContents);
System.out.println(diaryElement.getText());
%>
More information about the jdom-interest
mailing list