Apache的Java XMLRPC库

时间:2015-03-26 13:21:56

标签: java xml-rpc

我正在使用我的XML-RPC服务使用Apache XML-RPC库但是在响应XML-RPC时有垃圾字符,因此库无法解析结果

这是我的XML-RPC程序:

import java.net.URL;

import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;


public class XMLRpcExample {

    public static void main(String[] args) throws Exception{
        // TODO Auto-generated method stub

        XmlRpcClientConfigImpl cf = new XmlRpcClientConfigImpl();
        cf.setServerURL(new URL("/xmlrpc/object"));
        cf.setBasicUserName("admin");
        cf.setBasicPassword("m_demo");
        cf.setConnectionTimeout(60000);
        XmlRpcClient client = new XmlRpcClient();
        client.setConfig(cf);
        Object[] params = new Object[] {"dbname",1,"m_demo","res.partner","partner_sync_openerp","kapil5drd@bxiz","22"};
        String s =(String)client.execute("execute", params);
        System.out.println(s);
    }

}

但是我收到了这个错误,看起来像这样:

[Fatal Error] :16:16: An invalid XML character (Unicode: 0xc) was found in the element content of the document.
Exception in thread "main" org.apache.xmlrpc.client.XmlRpcClientException: Failed to parse server's response: An invalid XML character (Unicode: 0xc) was found in the element content of the document.
    at org.apache.xmlrpc.client.XmlRpcStreamTransport.readResponse(XmlRpcStreamTransport.java:202)
    at org.apache.xmlrpc.client.XmlRpcStreamTransport.sendRequest(XmlRpcStreamTransport.java:165)
    at org.apache.xmlrpc.client.XmlRpcHttpTransport.sendRequest(XmlRpcHttpTransport.java:125)
    at org.apache.xmlrpc.client.XmlRpcSunHttpTransport.sendRequest(XmlRpcSunHttpTransport.java:69)
    at org.apache.xmlrpc.client.XmlRpcClientWorker.execute(XmlRpcClientWorker.java:56)
    at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:167)
    at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:137)
    at org.apache.xmlrpc.client.XmlRpcClient.execute(XmlRpcClient.java:126)
    at XMLRpcExample.main(XMLRpcExample.java:21)
Caused by: org.xml.sax.SAXParseException; lineNumber: 16; columnNumber: 16; An invalid XML character (Unicode: 0xc) was found in the element content of the document.
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1237)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:551)
    at org.apache.xmlrpc.client.XmlRpcStreamTransport.readResponse(XmlRpcStreamTransport.java:200)
    ... 8 more

XML-RPC在响应中给出了一个垃圾字符。

并且库本身无法解析响应。

所以这意味着,Apache XML-RPC库是自己无法解析响应的。

任何身体都可以帮助我做我需要做的事吗?

我也尝试通过互联网修复此问题,但我无法解决。

1 个答案:

答案 0 :(得分:2)

以下是您的参数的工作示例,它可以帮助您

处理程序类:

public class Handler {
    public String execute(String dbName, Integer i, String a, String b, String c, String d, String e){
        System.out.println("Got inputs: "+dbName+", "+i+", "+a+", "+b+", "+c+", "+d+", "+e);
        return "<?xml version=\"1.0\"> <test>testmail@testdomain.com</test>";
    }
}

可以使用phm.addHandler("handler",Handler.class);向服务器代码添加更多此类处理程序。 可以向此类添加更多方法,并可以从客户端调用。

XMLRPC服务器:

import org.apache.xmlrpc.server.PropertyHandlerMapping;
import org.apache.xmlrpc.server.XmlRpcServer;
import org.apache.xmlrpc.server.XmlRpcServerConfigImpl;
import org.apache.xmlrpc.webserver.WebServer;

public class Server {
    private static final int port = 8080;

    public static void main(String[] args) throws Exception {
        WebServer webServer = new WebServer(port);
        XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
        PropertyHandlerMapping phm = new PropertyHandlerMapping();
        phm.addHandler("handler",Handler.class);
        xmlRpcServer.setHandlerMapping(phm);
        XmlRpcServerConfigImpl serverConfig =
                (XmlRpcServerConfigImpl) xmlRpcServer.getConfig();
        serverConfig.setEnabledForExtensions(true);
        serverConfig.setContentLengthOptional(false);
        webServer.start();
    }
}

XMLRPC客户端:

import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;

import java.net.URL;

public class Client {
    public static void main(String args[])throws Exception{
        XmlRpcClientConfigImpl cf = new XmlRpcClientConfigImpl();
        cf.setServerURL(new URL("http://localhost:8080/xmlrpc/object"));
        cf.setBasicUserName("admin");
        cf.setBasicPassword("m_demo");
        cf.setConnectionTimeout(60000);
        XmlRpcClient client = new XmlRpcClient();
        client.setConfig(cf);
        Object[] params = new Object[] {"dbname",1,"m_demo","res.partner","partner_sync_openerp","kapil5drd@bxiz","22"};
        String s =(String)client.execute("handler.execute", params);
        System.out.println(s);
    }
}