[jdom-interest] org.jdom.output.XMLOutputter in JDOM b8
Sullivan, Sean C - MLG
Sullivan.Sean at menlolog.com
Tue Jul 2 17:25:19 PDT 2002
I fixed my problem.
Here's my new code:
package foobar;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.jdom.*;
import java.io.*;
/**
*
*
*
*/
public class CustomXMLOutputter extends XMLOutputter
{
public CustomXMLOutputter()
{
super();
}
public java.lang.String escapeElementEntities(java.lang.String str)
{
return str;
}
public static void main(String[] args)
throws Exception
{
String xml = "<Foo><Bar><Blah>Hello</Blah></Bar></Foo>";
CustomXMLOutputter outputter = new CustomXMLOutputter();
outputter.setNewlines(true);
outputter.setLineSeparator("\r\n");
outputter.setIndent(" ");
outputter.setIndent(true);
Document doc = null;
SAXBuilder builder = new SAXBuilder(false);
doc = builder.build(new StringReader(xml));
String strResult = outputter.outputString(doc);
System.out.println(strResult);
}
}
> From: Sullivan, Sean C
>
>
> I am using JDOM b8
>
> My question is in regard to org.jdom.output.XMLOutputter
>
> First, I subclassed XMLOutputter
>
> Here is my implementation:
>
> /////////////////////////////////
>
> package foobar;
>
> import org.jdom.output.XMLOutputter;
>
> class CustomXMLOutputter extends XMLOutputter
> {
> public CustomXMLOutputter()
> {
> super();
> }
> public java.lang.String escapeElementEntities(java.lang.String str)
> {
> return str;
> }
> }
>
>
> /////////////////////////////////
>
>
>
> Here is how I am trying to use the XMLOutputter:
>
>
> ///////////////// Foo.java ////////////////////
> Public class Foo
> {
> public String toString()
> {
> CustomXMLOutputter outputter = new CustomXMLOutputter();
> outputter.setNewlines(true);
> outputter.setLineSeparator("\r\n");
> outputter.setIndent(" ");
> outputter.setIndent(true);
> String strResult =
> outputter.outputString(this.getXMLAsString());
> return strResult;
> }
>
> }
>
> ///////////////// end Foo.java ////////////////////
>
>
> Question:
>
>
> The CustomXMLOutputter is not formatting the code correctly.
>
> I am expecting the resulting string to have a "pretty" layout
> where the elements are on separate lines and have indentation.
>
> Unfortunately, the String that is returned contains 2 lines:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <XRequest><Common><RequestType>Hello</RequestType></Common></XRequest>
>
>
> How the elements are formatted this way?
>
> Is this a bug in JDOM or a bug in my code?
>
> -Sean
>
More information about the jdom-interest
mailing list