This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I don't understand the question but I think theres a MIME type text/xml. I don't see why it has to be treated differently. For example, a GET is a GET is a GET. Or maybe I don't understand the question...
Tony Alicea Senior Java Web Application Developer, SCPJ2, SCWCD
Joined: Apr 12, 2000
Sorry to be vague. Ok, let me try again: What is the best way for a Java client use HTTP to POST an XML document to a Java http servlet? Should the Java client send the XML document as a whole document or as a name/value pair? If it is sent as a name value/pair eg 'xmldoc=<the xml doc>' then the http servlet can retrieve the document by using getParameter("xmldoc") which return the content xml document. Is this an efficient way? What are other ways? Are there XML/Http libraries out there that does this? -- Josh
One of the popular free XML parsers is IBM's XML for Java (XML4J). It supports both DOM and SAX. You can use it with servlet. You can get it here. http://www.alphaworks.ibm.com/formula/xml My suggestion is to send the XML document (with DTD perhaps?) to the servlet and let the servlet parse the XML document and retrieve the data.
It depends if you intend to send any other data along with the XML. If your client/browser also needs to send cookies, hidden form fields, etc. etc. then you may need to use the name=value method. This is a "safe" approach, but can be considerably less efficient than sending "raw" XML over the HTML stream. Using name=value in a GET request is risky because some servers, proxies, firewalls etc. might restrict or truncate the length of a GET request, so it's inappropriate for flexible data format like XML. Using name=value in a POST request incurs the penalty of URL-escaping the data at the client and unescaping it at the server, even though XML has its own processes for that. The most efficient method is to send the XML as the only data over the stream and have a custom servlet to read the input. You may also want to use some form of "content-length" to simplify reading the data at the server end. Conceptually XML should be an alternative HTML request format, understood by servers and clients so you could say something like <form action=/myapp/servlet/something method=XML> blah ... </form> but until that becomes popular, you have to roll your own.