webservice接口例子源码



webservice接口例子源码。

接口返回来的xml字符串jkdata.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<DataTable xmlns=”http://sys.cdxinyong.com”>
  <xs:schema id=”NewDataSet” xmlns=”" xmlns:xs=”http://www.w3.org/2001/XMLSchema” xmlns:msdata=”urn:schemas-microsoft-com:xml-msdata”>
    <xs:element name=”NewDataSet” msdata:IsDataSet=”true” msdata:MainDataTable=”Table” msdata:UseCurrentLocale=”true”>
      <xs:complexType>
        <xs:choice minOccurs=”0″ maxOccurs=”unbounded”>
          <xs:element name=”Table”>
            <xs:complexType>
              <xs:sequence>
                <xs:element name=”Id” type=”xs:int” minOccurs=”0″ />
                <xs:element name=”GUID” type=”xs:string” minOccurs=”0″ />
                <xs:element name=”GName” type=”xs:string” minOccurs=”0″ />
                <xs:element name=”BId” type=”xs:int” minOccurs=”0″ />
                <xs:element name=”LastDate” type=”xs:string” minOccurs=”0″ />
                <xs:element name=”Status” type=”xs:unsignedByte” minOccurs=”0″ />
                <xs:element name=”StatusDis” type=”xs:string” minOccurs=”0″ />
                <xs:element name=”BiaoDuanName” type=”xs:string” minOccurs=”0″ />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <diffgr:diffgram xmlns:msdata=”urn:schemas-microsoft-com:xml-msdata” xmlns:diffgr=”urn:schemas-microsoft-com:xml-diffgram-v1″>
    <NewDataSet xmlns=”">
      <Table diffgr:id=”Table1″ msdata:rowOrder=”0″>
        <Id>166</Id>
        <GUID>1002000100000000000000001014100</GUID>
        <GName>皇冠国际社区四期总图幼儿园大门</GName>
        <BId>7814</BId>
        <LastDate>2013-03-11</LastDate>
        <Status>1</Status>
        <StatusDis>在线</StatusDis>
        <BiaoDuanName>皇冠国际社区四期总图、幼儿园</BiaoDuanName>
      </Table>
      <Table diffgr:id=”Table2″ msdata:rowOrder=”1″>
        <Id>167</Id>
        <GUID>1002000100000000000000001014101</GUID>
        <GName>皇冠国际社区四期总图刷卡机</GName>
        <BId>0</BId>
        <LastDate>2013-03-11</LastDate>
        <Status>1</Status>
        <StatusDis>在线</StatusDis>
      </Table>
     </NewDataSet>
  </diffgr:diffgram>
</DataTable>
package com.cdmcs.jksp.client.update;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.VisitorSupport;
public class SurveyServiceUtil extends VisitorSupport{
private String GName;
public String getGName() {
return GName;
}
public void setGName(String name) {
GName = name;
}
public void visit(Element node) {
        if (“GName”.equals(node.getName())) {
            this.setGName(node.getText());
            try{
             System.out.println(“GName===”+new String(node.getText().getBytes(“iso8859-1″),”gbk”));
            }catch(Exception e){
             e.printStackTrace();
            }
        } else{
        }
    }
public void analysis(String soapContent) throws DocumentException {
    Document doc = (Document) DocumentHelper.parseText(soapContent);
    doc.accept(this);
}
}
package com.cdmcs.jksp.client.update;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
public class Test {
/**
 * @param args
 */
public static void main(String[] args)  throws HttpException, IOException {
String soapRequestData = “<?xml version=\”1.0\” encoding=\”utf-8\”?>”;
soapRequestData += “<soap12:Envelope xmlns:xsi=\”http://www.w3.org/2001/XMLSchema-instance\” xmlns:xsd=\”http://www.w3.org/2001/XMLSchema\” xmlns:soap12=\”http://www.w3.org/2003/05/soap-envelope\”>”;
soapRequestData += “<soap12:Body>”;
soapRequestData += “<GetVideos  xmlns=\”http://sys.cdxinyong.com\” />”;
soapRequestData += “</soap12:Body>”;
soapRequestData += “</soap12:Envelope>”;
System.out.println(soapRequestData);
PostMethod postMethod = new PostMethod(“http://localhost/jkdata.xml”);
postMethod.setRequestHeader(“Content-Type”, “text/xml; charset=UTF-8″);
byte[] b = soapRequestData.getBytes(“utf-8″);
InputStream is = new ByteArrayInputStream(b,0,b.length);
RequestEntity re = new InputStreamRequestEntity(is,b.length,”application/soap + xml; charset=utf-8″);
postMethod.setRequestEntity(re);
HttpClient httpClient = new HttpClient();
int statusCode = httpClient.executeMethod(postMethod);
System.out.println(“statuscode=” + statusCode);
String soapResponseData = postMethod.getResponseBodyAsString();
//System.out.println(“soapResponseData=” + soapResponseData);
SurveyServiceUtil util = new SurveyServiceUtil();
try{
util.analysis(soapResponseData);
}catch(Exception e){
e.printStackTrace();
}
}
}