Summary


XML documents are text. This means that you can create an XML document using any technique you would normally use to create text filesincluding 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. But, doing this is not hard, certainly no harder than debugging any other part of a program, and often considerably easier.

Because XML documents are text, it's best to use a Writer rather than an OutputStream to generate an XML document. However, because the Writer class has no standard method of determining what encoding it's using, you should normally build your Writer from an OutputStream you control, which you chain to an OutputStreamWriter (rather than accepting a Writer of unknown provenance). This is the only way to ensure 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. You can send the request just by writing the XML document representing the request onto the URLConnection 's output stream, and then flushing. The response will return 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.



Processing XML with Java. A Guide to SAX, DOM, JDOM, JAXP, and TrAX
Processing XML with Javaв„ў: A Guide to SAX, DOM, JDOM, JAXP, and TrAX
ISBN: 0201771861
EAN: 2147483647
Year: 2001
Pages: 191

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net