This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
Author

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,

Mike

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

Joined: Mar 16, 2001
Posts: 984

> 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? http://xml.apache.org/xerces2-j/faq-dom.html#faq-4


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

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 java.io.StringWriter;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

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
Bartender

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.,
Balaji
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
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
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.
Bill
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,
Mike
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting XML Out of a org.w3c.dom.Document
 
Similar Threads
JAXP Not Creating Nodes
XML element
Can anyone help?I still can't append records in xml...
Invalid XML error...Newbie question
Generating XML from java