File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes Getting XML Out of a org.w3c.dom.Document Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Getting XML Out of a org.w3c.dom.Document" Watch "Getting XML Out of a org.w3c.dom.Document" New topic

Getting XML Out of a org.w3c.dom.Document

Mike Litherland
Ranch Hand

Joined: Aug 12, 2004
Posts: 31
Hi all,

I'm going to ask a multi-part question and perhaps I can get responses regarding perhaps other clever ways to handle what I'm trying to do.

Basically I'm writing a simple chat server in Java, where the server exchanges information to clients over HTTP using XML. That way the clients can either be Java Swing apps or jsp pages or whatever else you can think of.

The way I've approached the problem is to create my own XML Documents using org.w3c.dom.Document and the DocumentBuilder.newDocument method. Works great, but I don't know how to get a string representation of the XML document out of the object to send back as part of the http request. Am I missing something obvious here?

Second, this project is just a learning project for me - something fun to do. I know I could use Axis to exchange information instead of hand-rolled XML, but I could also use plain text and parse it on both ends. I'm looking basically for a clever way to do this without SOAP, or XMLRPC. I know, I know, reinventing the wheel and all that, but again, this is for fun. So basically, I'm wondering how you (yes, you) would approach this project, particularly if you think my way is dumb. I'm also wondering about the suitability of something like XMLBeans instead of using the DOM interface for the XML. Any thoughts or recommendations?

Thanks for your time,


P.S. This is how I create my Document.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

> I don't know how to get a string representation of the XML document out of the object to send back as part of the http request.

This help?

Please ignore post, I have no idea what I am talking about.
Greg Jewell

Joined: Mar 08, 2002
Posts: 10
I just started using w3c DOM the other day and encountered the same problem. (I had previously used jdom and dom4j.) The solution I found to convert a Document to a String follows. There may be other (easier) methods, but this is the route I used. Note that this uses JAXP from Sun's 1.4.2 SDK.

import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.dom.DOMSource;

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
DOMSource source = new DOMSource(document);
StringWriter stringWriter = new StringWriter();
StreamResult result = new StreamResult(stringWriter);
transformer.transform(source, result);
String xmlString = stringWriter.toString();
Balaji Loganathan
author and deputy

Joined: Jul 13, 2001
Posts: 3150
This topic is not related to webservices.
I am moving this post to our XML discussion forum.
Please continue your discussion there.
Thank you.,
Mike Litherland
Ranch Hand

Joined: Aug 12, 2004
Posts: 31
Thanks for the help. Greg's solution is along the lines of what I was looking for, but I'm disappointed by its complexity (of course sometimes I'm disappointed by Java's complexity, so it follows. ).

I can tuck this complexity away in an object so it's not that big of a deal. But I'd like to think an elegant solution exists somewhere. Does JDOM handle this in a simpler way? XMLBeans looks promising the more I look into it. Anything else worth considering, it doesn't have to be XML at all.

Thanks again for the help.
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
By the way, this is a faq.

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
Complexity??? It is 7 lines of code and some import statements!
The Java XML library is considerably refined from the first versions. The way you can mix Source and Result to/from various forms is a real jewel IMHO.
I don't think there is a "Royal Road" to XML.
Mike Litherland
Ranch Hand

Joined: Aug 12, 2004
Posts: 31
Thanks, all for the input. What I think I'm going to do is switch to JDOM. This is what the previous code looks like using JDOM, which IMO is easier to read and understand. I like how the seven lines of code with five new objects created becomes two lines with one new object created when you get to the point you want to output the thing.

Thanks again,
I agree. Here's the link:
subject: Getting XML Out of a org.w3c.dom.Document
It's not a secret anymore!