[jdom-interest] Question on deprecated XMLOutputter method
Mark J Laird
Markl at waterford.org
Mon Jul 30 09:34:31 PDT 2001
All,
I am updating my code to use jdom beta7. In the process, I have had to change several parts of XMLOutputter
code to remove deprecated methods. I am having some difficulties rewriting the code that was using the
"setIndentLevel" method. I am using the code to add one Element to an existing XML file, and I want the
Element to be "pretty-print" indented so that it lines up with the existing Elements in the file. The existing
XML file is a log file, which can get quite long, so I don't want to read the entire file into memory when I
know that I will always be adding the newest entry to the end of the file.
The documentation indicates that a "stacked FilterOutputStream" is the preferred solution. Could someone give
me some insights on how this might be done, or point me to some resources that would help me? I have included
part of my code below to show what I am doing. Thanks in advance.
Mark
[Sample code]
File logFile;
XMLOutputter xmlOut = new XMLOutputter(OUTPUT_INDENTATION,true);
xmlOut.setOmitDeclaration(true);
xmlOut.setExpandEmptyElements(true);
Element xmlLogEntry = createLogEntryXML(strMessage,t,strEntryType,additionalInfo);
//Append the newly created error to the end of the existing file. This
//requires that we remove the closing </Logs> and </ErrorLog> tags, add
//the new error, and replace the closing tags.
removeClosingTagsFromFile(LOGS_TAG_NAME,1,logFile);
FileOutputStream os = new FileOutputStream(logFile.getPath(),true); //The second parameter signals that
we append to the file rather than overwrite
xmlOut.setIndentLevel(2); //Leading spaces for the error XML tags
xmlOut.output(xmlLogEntry,os);
String strClosingTags = OUTPUT_INDENTATION + "</" + LOGS_TAG_NAME + ">\r\n" +
"</" + ERROR_LOG_TAG_NAME + ">\r\n";
os.write(strClosingTags.getBytes());
os.close();
More information about the jdom-interest
mailing list