Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to change JDOM Document into String?

 
rick collette
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, guys:
I am struggling to do the above, how
can I achieve that?
regards,
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried org.jdom.Document.toString() ?
 
rick collette
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Valentin Crettaz:
Have you tried org.jdom.Document.toString() ?

Thanks, Valentin. toString does not work, only gives you
root element and ns.
I figured it out, the follwoing will work:
StringWriter writer = new StringWriter();
TransformerFactory tf = TransformerFactory.newInstance();
Transformer tran = tf.newTransformer();
Source src = new DOMSource(doc);
Result res = new StreamResult(writer);
tran.transform(src, res);
String xml = writer.toString();
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic