XML-two systems use servlet to send and parse XML

Source: Internet
Author: User

Example:

Line 2 of the code opens an address connection and returns the httpurlconnection object.

The XML content to be transmitted in line 3 is written to the httpurlconnection
Outputstream stream. (Note that byte arrays need to be input here)

Then call. getinputstream () In line 23 will jump to the server servlet. If you do not call. getinputstream (), it will not jump to the past.

Client:

Package adopen; import Java. io. inputstream; import Java. io. outputstreamwriter; import java.net. httpurlconnection; import java.net. URL; import COM. haier. util. aesutil; import COM. haier. util. streamtool; public class urltest {public static string testservleturl (string path, string XML) throws exception {URL url = new URL (PATH); httpurlconnection conn = (httpurlconnection) URL. openconnection (); Conn. setrequestmet Hod ("Post"); // submit mode Conn. setconnecttimeout (10000); // connection timeout unit: millisecond Conn. setreadtimeout (5000); // read timeout in milliseconds Conn. setdooutput (true); // whether to input the parameter // use Conn. getoutputstream (). write writes XML Information to the system at the other end, get it and parse Conn. getoutputstream (). write (XML. getbytes ("UTF-8"); inputstream instream = Conn. getinputstream (); instream. close (); Return XML;} public static string getcampaginxmlinfo () {stringbuilder sb = new stringbuilder (); sb. Append ("<? XML version = \ "1.0 \" encoding = \ "UTF-8 \"?> "); Sb. append ("

The XML file sent from the client is parsed using Dom. For details, see XML -- Parse XML using dom

Note: I use servlet3.0 here to replace the configuration in Web. XML with annotations.

@ Webservlet (urlpatterns = {"/domtestservlet"}, asyncsupported = true). If you are not a servlet3.0 version, configure the servlet jump information in Web. xml.

Server:

Package COM. haier. web; import Java. io. ioexception; import javax. servlet. servletexception; import javax. servlet. annotation. webservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import javax. XML. parsers. documentbuilder; import javax. XML. parsers. documentbuilderfactory; import javax. XML. parsers. parserconfigurationexception; import Org. w3C. dom. domexception; import Org. w3C. dom. document; import Org. w3C. dom. element; import Org. w3C. dom. namednodemap; import Org. w3C. dom. node; import Org. w3C. dom. nodelist; import Org. XML. sax. saxexception; /*** use recursion to parse any given XML document and output its content to the command line ** @ author zhanglong **/@ webservlet (urlpatterns = {"/domtestservlet" }, extends = true) public class domtestservlet extends {protected void doget (httpservletrequest request, incluresponse) throws servletexception, ioexception {dopost (request, response);} protected void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {try {// Step 1: Get the DOM parser factory (which works to create a specific parser) documentbuilderfactory DBF = documentbuilderfactory. newinstance (); // system. out. println ("Class Name:" + DBF. getclass (). getname (); // Step 2: Obtain the specific Dom parser documentbuilder DB = DBF. newdocumentbuilder (); // system. out. println ("Class Name:" + dB. getclass (). getname (); // Step 3: parse an XML document and obtain the Document Object (root node) document = dB. parse (request. getinputstream (); nodelist list = document. getelementsbytagname ("haier_ad"); For (INT I = 0; I <list. getlength (); I ++) {element = (element) list. item (I); string content = element. getelementsbytagname ("campname "). item (0 ). getfirstchild (). getnodevalue (); system. out. println ("campname:" + content); content = element. getelementsbytagname ("agent_id "). item (0 ). getfirstchild (). getnodevalue (); system. out. println ("agent_id:" + content); content = element. getelementsbytagname ("client_id "). item (0 ). getfirstchild (). getnodevalue (); system. out. println ("client_id:" + content); content = element. getelementsbytagname ("coverid "). item (0 ). getfirstchild (). getnodevalue (); system. out. println ("coverid:" + content) ;}} catch (domexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (parserconfigurationexception e) {// todo auto-generated catch blocke. printstacktrace ();} catch (saxexception e) {// todo auto-generated catch blocke. printstacktrace ();}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.