• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

DOM contents to string instead of a file...

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I would like to write the contents of a DOM to a string instead of to a file. Does any one have an example of doing this?
Thanks in advance.
Mike Cronin
Data On Call
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JavaRanch Wiki to the rescue
 
Mike Cronin
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Lasse,
Thanks for the quick response!
I went with the following code which works great!

import org.apache.xml.serialize.*;
...
private void domToString(Document doc) throws Exception{
try {
// Serialize the DOM
OutputFormat format = new OutputFormat(doc);
// Establish a new StringWriter object
StringWriter sw = new StringWriter();
// Establish a XMLSerializer object
XMLSerializer serial = new XMLSerializer(sw, format);
// Set the XMLSerializer as a DOM Serializer
serial.asDOMSerializer();
// Serialize the DOM
serial.serialize(doc.getDocumentElement());
// Store the DOM contents to a string
requestXML = sw.toString();
}
catch (Exception e) {
System.out.println("Error in domToString method " + e.getMessage());
throw new Exception ("Error in domToString method" + e);
}
}

Do you have an opion on using the javax.xml libraries as opposed to org.apache.xml?
Thanks again for the quick response !
Mike Cronin
Data On Call
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Cronin:
Do you have an opion on using the javax.xml libraries as opposed to org.apache.xml?

Only the obvious: org.apache.xml.* is not a standard while javax.xml.* is... If you're ok with relying on having Xerces as your parser, the XMLSerializer probably works better than the Transformer trick, though.
 
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic