未解决的编译问题

时间:2014-04-21 07:59:01

标签: java soap compilation

我在java中使用Web服务客户端来调用soap web服务,我添加了如下代码,但我在<span class="skimlinks-unlinked">

的地方收到错误

请帮帮我..

我们必须为此

使用任何jar文件
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;

public class SOAPClient {

    private static final String endpoint = "http://localhost/SOAPService/MySoapService";

    public static void main(String[] args) throws SOAPException {
        SOAPMessage message = MessageFactory.newInstance().createMessage();
        SOAPHeader header = message.getSOAPHeader();
        header.detachNode();

        SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
        envelope.setAttribute("namespace","namespaceUrl");

        SOAPBody body = message.getSOAPBody();
        QName bodyName = new QName("getResponse");
        SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
        SOAPElement symbol = bodyElement.addChildElement("name");
        symbol.addTextNode("Harry Joy");

        SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
        SOAPMessage response = <span class="skimlinks-unlinked">connection.call(message</span>, endpoint);
        <span class="skimlinks-unlinked">connection.close</span>();

        SOAPBody responseBody = response.getSOAPBody();
        SOAPBodyElement responseElement = (SOAPBodyElement)responseBody.getChildElements().next();
        SOAPElement returnElement = (SOAPElement)responseElement.getChildElements().next();
        if(responseBody.getFault()!=null){
            <span class="skimlinks-unlinked">System.out.println(returnElement.getValue</span>()+" "+responseBody.getFault().getFaultString());
        } else {
            <span class="skimlinks-unlinked">System.out.println(returnElement.getValue</span>());
        }

        try {
            <span class="skimlinks-unlinked">System.out.println(getXmlFromSOAPMessage(message</span>));
            <span class="skimlinks-unlinked">System.out.println(getXmlFromSOAPMessage(response</span>));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static String getXmlFromSOAPMessage(SOAPMessage msg)
            throws SOAPException, IOException {
        ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
        msg.writeTo(byteArrayOS);
        return new String(byteArrayOS.toByteArray());
    }

}

1 个答案:

答案 0 :(得分:0)

您不能将XML标记放在Java文件中,您必须将其删除,它们不是Java语言。

我为你删除了,试试这个:

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;

public class SOAPClient {

    private static final String endpoint = "http://localhost/SOAPService/MySoapService";

    public static void main(String[] args) throws SOAPException {
        SOAPMessage message = MessageFactory.newInstance().createMessage();
        SOAPHeader header = message.getSOAPHeader();
        header.detachNode();

        SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
        envelope.setAttribute("namespace","namespaceUrl");

        SOAPBody body = message.getSOAPBody();
        QName bodyName = new QName("getResponse");
        SOAPBodyElement bodyElement = body.addBodyElement(bodyName);
        SOAPElement symbol = bodyElement.addChildElement("name");
        symbol.addTextNode("Harry Joy");

        SOAPConnection connection = SOAPConnectionFactory.newInstance().createConnection();
        SOAPMessage response = connection.call(message, endpoint);
        connection.close

        SOAPBody responseBody = response.getSOAPBody();
        SOAPBodyElement responseElement = (SOAPBodyElement)responseBody.getChildElements().next();
        SOAPElement returnElement = (SOAPElement)responseElement.getChildElements().next();
        if(responseBody.getFault()!=null){
            System.out.println(returnElement.getValue()+" "+responseBody.getFault().getFaultString());
        } else {
            System.out.println(returnElement.getValue());
        }

        try {
            System.out.println(getXmlFromSOAPMessage(message));
            System.out.println(getXmlFromSOAPMessage(response));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static String getXmlFromSOAPMessage(SOAPMessage msg)
            throws SOAPException, IOException {
        ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
        msg.writeTo(byteArrayOS);
        return new String(byteArrayOS.toByteArray());
    }

}