How to return XML from a Servlet

R

ryan.marples

Hi folks,

I am writing a set of servlets that will return XML based on some
backend logic. I have used the DOM method and after my processing am
left with a org.w3c.dom.Document object that I want to stream down to
the client.

Seems simple but I'm not clear how to actually send this xml document
to the response stream. I found a code example that used the
javax.xml.transform.Transformer but am getting an error about
namespaces when I try this. I don't really want to "transform" the xml,
just write it out, so is this really the right method to use? Isn't
there just an XmlWriter class or something that given a stream and
write the doc out to?
 
C

Chris Smith

I am writing a set of servlets that will return XML based on some
backend logic. I have used the DOM method and after my processing am
left with a org.w3c.dom.Document object that I want to stream down to
the client.

Seems simple but I'm not clear how to actually send this xml document
to the response stream. I found a code example that used the
javax.xml.transform.Transformer but am getting an error about
namespaces when I try this. I don't really want to "transform" the xml,
just write it out, so is this really the right method to use? Isn't
there just an XmlWriter class or something that given a stream and
write the doc out to?

Using the transformation classes is indeed the only way to do this
portably using JAXP. What error are you getting? The term "error about
namespaces" isn't really descriptive.

Alternatively, if you're willing to marry a specific parser such as
Xerces, it's got a simple method to write out XML over a stream.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,629
Members
45,332
Latest member
LeesaButts

Latest Threads

Top