dcsimg
How to Convert a MSXML DOM doc into a string
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Vidya_Kirani
Posted On:   Thursday, May 9, 2002 01:48 PM

Re: How to Convert a MSXML DOM doc into a string

Posted By:   Michael_Aird  
Posted On:   Friday, May 10, 2002 07:28 AM

I don't know if this is the best way (I'm pretty sure I copied this from somewhere) but this is the code I use:

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;



try{
// Use a Transformer for output
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();

DOMSource source = new DOMSource(document);

StringWriter theWriter=new StringWriter();

StreamResult result = new StreamResult(theWriter);
transformer.transform(source, result);
this.setContent(theWriter.toString());
}
catch (TransformerConfigurationException tce) {
// Error generated by the parser
System.out.println("* Transformer Factory error");
System.out.println(" " + tce.getMessage() );

// Use the contained exception, if any
Throwable x = tce;
if (tce.getException() != null)
x = tce.getException();
x.printStackTrace();

}
catch (TransformerException te) {
// Error generated by the parser
System.out.println("* Transformation error");
System.out.println(" " + te.getMessage() );

// Use the contained exception, if any
Throwable x = te;
if (te.getException() != null)
x = te.getException();
x.printStackTrace();
}


where document is an XML DOM Document.
About | Sitemap | Contact