[jdom-interest] newbie

Mark C. Stafford obbyyoyo at gmail.com
Fri Oct 29 13:50:13 PDT 2004


On 29 Oct 2004 19:20:49 -0000, pamarti  srikiran
<psrikiran at rediffmail.com> wrote:
> 
> Hi I recently downloaded jdom 1.0 and tried to use it. I m unable
> to get the newline to be printed in the output xml file.
> Im getting the following output
> <sri>hi</sri>
> 
> instead of
> <sri>
> hi
> </sri>
> 
> can anybody tell me how to do this. i have examples that use
> oldversion of jdom. help is greatly appreciated
> 
> thanks
> -sri
>

I think that there were too few items to trigger proper prettiness, so
I added one. Is this what you want?

import org.jdom.*;
import org.jdom.output.*;
/*
  http://jdom.org/docs/apidocs/org/jdom/Element.html
  http://jdom.org/docs/apidocs/org/jdom/output/Format.html
  http://jdom.org/docs/apidocs/org/jdom/output/XMLOutputter.html
*/
class foo {
  public static void main(String mainArgs[]) {
    Element foo1 = new Element("stuff");
    Element foo2 = new Element("sri");
    foo2.setText("hi");
    foo1.setContent(foo2);
    Element foo3 = (Element)foo1.clone();
    Format f = Format.getPrettyFormat();
    Format r = Format.getRawFormat();
    r.setOmitDeclaration(true); // leaves out <?xml version="1.0"
encoding="UTF-8"?>
    XMLOutputter a = new XMLOutputter(f);
    XMLOutputter b = new XMLOutputter(r);
    String bar = a.outputString(new Document(foo1));
    System.out.println(bar);
    String zee= b.outputString(new Document(foo3));
    System.out.println(zee);
    /*
      <?xml version="1.0" encoding="UTF-8"?>
      <stuff>
        <sri>hi</sri>
      </stuff>
      
      
      <stuff><sri>hi</sri></stuff>
    */
  }
}


More information about the jdom-interest mailing list