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?
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.
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
Joined: Aug 12, 2004
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.
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
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.