Summary

XML documents are text. This means you can create an XML document using any technique you would normally use to create text files including but not limited to output streams, writers, toString() methods, char arrays, or anything else that produces text. When you do this, you’re responsible for maintaining well-formedness and validity. However, doing this is not hard, certainly no harder than debugging any other part of a program and often considerably easier.

Since XML documents are text, you should use a Writer rather than an OutputStream to generate an XML document. However, since the Writer class doesn’t have any standard method of determining what encoding it’s using, you should normally chain your Writer to an OutputStream you control which you immediately chain to an OutputStreamWriter rather than accepting a Writer of unknown provenance from somewhere else. This is the only way to make sure that the encoding declaration matches what you’re actually outputting.

On the client side, the java.net.URLConnection class is needed to talk to XML-RPC and SOAP servers because it allows you to use the HTTP POST method. The request can be sent just by writing the XML document representing the request onto the URLConnection’s output stream, and then flushing. The response returns as an XML document also. Future chapters will address the parsing of such response documents.

On the server side, Java servlets serve XML just as easily as they serve HTML, You simply have to remember to set the MIME media type of the response to text/xml and include an encoding in the MIME type if you want anything other than Latin-1. You write the XML document onto the servlet’s OutputStream just as you’d write an HTML document. There’s really nothing special about servlets in this respect, other than that this is a book about Java. Similar techniques work for CGIs written in C, AppleScript, or Perl or for other server side programming environments such as ASP and PHP.


Copyright 2001, 2002 Elliotte Rusty Haroldelharo@metalab.unc.eduLast Modified May 24, 2002
Up To Cafe con Leche