Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache's Java XMLRPC library

Tags:

java

xml-rpc

I am using my XML-RPC service using Apache XML-RPC library but in reponse of XML-RPC has junk character so library can not parse the result

Here, is my XML-RPC program:

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);
    }

}

But I am getting this error in response, which looks like this:

[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

The XML-RPC gives a junk character in reponse.

And the library itself fails to parse the response.

So it means, Apache XML-RPC library is it self unable to parse the response.

Can any body help me with what I need to do?

I have also tried to fix this issue via the internet but I am unable to solve it.

like image 272
kapil nadiyapara Avatar asked Mar 26 '15 13:03

kapil nadiyapara


People also ask

What does XML-RPC stand for?

XML-RPC (short for Extensible Markup Language remote procedure call) is a protocol specification for executing RPC calls (remote calls in computer networks) using the stateless network protocol HTTP and the markup language XML, which gives it part of its name.

What is XML-RPC in Linux?

XML-RPC is a quick-and-easy way to make procedure calls over the Internet. It converts the procedure call into an XML document, sends it to a remote server using HTTP, and gets back the response as XML. This library provides a modular implementation of XML-RPC for C and C++.

What is XML-RPC Python?

XML-RPC is a Remote Procedure Call method that uses XML passed via HTTP as a transport. With it, a client can call methods with parameters on a remote server (the server is named by a URI) and get back structured data. xmlrpc is a package that collects server and client modules implementing XML-RPC.

What is XML-RPC and JSON RPC?

It is similar to the XML-RPC protocol, defining only a few data types and commands. JSON-RPC allows for notifications (data sent to the server that does not require a response) and for multiple calls to be sent to the server which may be answered asynchronously.


1 Answers

Here is the working example for your parameters, which can help you


Handler Class:

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>[email protected]</test>";
    }
}

More such handlers can be added using phm.addHandler("handler",Handler.class); to the server code. More methods can be added to this class and can be called from client.


XMLRPC Server:

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 Client:

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);
    }
}
like image 90
Vitthal Kavitake Avatar answered Sep 29 '22 01:09

Vitthal Kavitake