Converting DOM XML to String

E

esor50

I am making my first try at creating XML and passing it to a
SOAPServer. I am using java, importing the following import
declarations:

import java.sql.*;
import javax.xml.parsers.*;
import javax.xml.rpc.*;
import org.w3c.dom.*;

I create and populate the XML in a document. Then I find that, in
order to put my XML into a String, I need to serialize the document to
a file. I then read the string from the file. This approach is
working but it seems crazy to me; is there a way to convert the XML in
the document to a String without putting it into a file?
Thanks
 
P

Paul J. Lucas

esor50 said:
I create and populate the XML in a document. Then I find that, in
order to put my XML into a String, I need to serialize the document to
a file. I then read the string from the file. This approach is
working but it seems crazy to me; is there a way to convert the XML in
the document to a String without putting it into a file?

Something like:

byte[] bytes = myXMLString.getBytes( "UTF-8" );
ByteArrayInputStream bis = new ByteArrayInputStream( bytes );
StreamSource source = new StreamSource( bis );
DocumentBuilder builder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.newDocument();
DOMResult result = new DOMResult( doc );
Transformer xform = TransformerFactory.newInstance().newTransformer();
xform.transform( source, result );
return doc;

This was a copy/paste from working code, so it's not guaranteed to be 100%
correct, but it'll point you in the right direction.

- Paul

P.S.: It seems like an aweful lot of work, doesn't it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top