Java xml rpc 访问openerp的详细例子
-
群里面问了一大转,还是没有找到java怎么调用xml的例子....
无语,只能自己参考doc.openerp.com研究了。
这里面只有java xml rpc的login和list db例子,但是重要的对象读写操作却没有涉及到。
分享一下我测试的例子,javaer不用再碰墙了...
简单起见,这里定义了自己的对象x_test,只有两个属性,x_name和x_desc,均为char
简单起见,直接贴代码了...
更新说明一下,这里需要使用apache的xml rpc包,请到apache的网站下载,加上就ok了。<br />import java.net.MalformedURLException;<br />import java.net.URL;<br />import java.util.ArrayList;<br />import java.util.Arrays;<br />import java.util.HashMap;<br />import java.util.List;<br />import java.util.Vector;<br /><br />import org.apache.xmlrpc.XmlRpcException;<br />import org.apache.xmlrpc.client.XmlRpcClient;<br />import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;<br /><br /><br />public class xmltest {<br /><br /> /**<br /> * @param args<br /> * @throws MalformedURLException<br /> */<br /> public static void main(String[] args) throws MalformedURLException {<br /> // TODO Auto-generated method stub<br /> // getDatabaseList("127.0.0.1",8069);<br /><br /> // createObject();<br /> // searchObject();<br /> // readData();<br /> readView();<br /> }<br /><br /> public static Vector<String> getDatabaseList(String host, int port)<br /> throws MalformedURLException {<br /> XmlRpcClient xmlrpcDb = new XmlRpcClient();<br /><br /> XmlRpcClientConfigImpl xmlrpcConfgDb = new XmlRpcClientConfigImpl();<br /> xmlrpcConfgDb.setEnabledForExceptions(true);<br /> xmlrpcConfgDb.setServerURL(new URL("http", host, port, "/xmlrpc/db"));<br /><br /> xmlrpcDb.setConfig(xmlrpcConfgDb);<br /> Vector<String> res = new Vector<String>();<br /> try {<br /> Vector<Object> params = new Vector<Object>();<br /> Object result = xmlrpcDb.execute("list", params);<br /> Object[] a = (Object[]) result;<br /><br /> for (int i = 0; i < a.length; i++) {<br /> res.addElement((String) a[i]);<br /> System.out.println((String) a[i]);<br /> }<br /><br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> return res;<br /> }<br /><br /> public int Connect(String host, int port, String tinydb, String login,<br /> String password) throws Exception {<br /> XmlRpcClient xmlrpclogin = new XmlRpcClient();<br /><br /> XmlRpcClientConfigImpl xmlrpcConfigLogin = new XmlRpcClientConfigImpl();<br /><br /> xmlrpcConfigLogin.setEnabledForExceptions(true);<br /> xmlrpcConfigLogin.setServerURL(new URL("http", host, port,<br /> "/xmlrpc/common"));<br /><br /> xmlrpclogin.setConfig(xmlrpcConfigLogin);<br /><br /> try {<br /> Object[] params = new Object[] { tinydb, login, password };<br /> Object id = xmlrpclogin.execute("login", params);<br /> if (id instanceof Integer)<br /> return (Integer) id;<br /> return -1;<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /><br /> return -1;<br /> }<br /><br /> public static void createObject() throws MalformedURLException {<br /> XmlRpcClient xmlrpc = new XmlRpcClient();<br /><br /> XmlRpcClientConfigImpl xmlrpcConfgDb = new XmlRpcClientConfigImpl();<br /> xmlrpcConfgDb.setEnabledForExceptions(true);<br /> xmlrpcConfgDb.setServerURL(new URL(<br /> "http://localhost:8069/xmlrpc/object"));<br /><br /> xmlrpc.setConfig(xmlrpcConfgDb);<br /><br /> String dbname = "openerp";<br /> int uid = 1;<br /> String pwd = "000000";<br /> String model = "x_test";<br /><br /> HashMap<String, Object> values = new HashMap<String, Object>();<br /> values.put("x_name", "Monsieur");<br /> // values.put("x_desc", "bbbb");<br /><br /> // Object[] params = new Object[] { dbname, uid, pwd, model,"create",<br /> // values };<br /><br /> Object[] params = new Object[] { "openerp", 1, "000000", "x_test",<br /> "create", values };<br /><br /> try {<br /> xmlrpc.execute("execute", params);<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /> }<br /> }<br /><br /> public static void searchObject() throws MalformedURLException {<br /> XmlRpcClient xmlrpc = new XmlRpcClient();<br /><br /> XmlRpcClientConfigImpl xmlrpcConfgDb = new XmlRpcClientConfigImpl();<br /> xmlrpcConfgDb.setEnabledForExceptions(true);<br /> xmlrpcConfgDb.setServerURL(new URL(<br /> "http://localhost:8069/xmlrpc/object"));<br /><br /> xmlrpc.setConfig(xmlrpcConfgDb);<br /><br /> String dbname = "openerp";<br /> int uid = 1;<br /> String pwd = "000000";<br /> String model = "x_test";<br /> /*<br /> * ArrayList<String> l = new ArrayList<String>(); l.add("x_name");<br /> * l.add("="); l.add("a"); ArrayList filters = new ArrayList();<br /> */<br /> Object[] l = { "x_name", "=", "a" };<br /> Object[] filters = { l };<br /> // filters.add(l);<br /> Object[] params = new Object[] { "openerp", 1, "000000", "x_test",<br /> "search", filters };<br /><br /> try {<br /><br /> Object[] o = (Object[]) xmlrpc.execute("execute", params);<br /><br /> System.out.println("finished");<br /> // System.out.println(o.length);<br /> for (Object obj : o) {<br /> System.out.println(obj);<br /> }<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /><br /> }<br /> }<br /><br /> public static void readData() throws MalformedURLException {<br /> Object[] ids = { 2, 3 };<br /> Object[] fields = { "x_name", "x_desc" };<br /><br /> XmlRpcClient xmlrpc = new XmlRpcClient();<br /><br /> XmlRpcClientConfigImpl xmlrpcConfgDb = new XmlRpcClientConfigImpl();<br /> xmlrpcConfgDb.setEnabledForExceptions(true);<br /> xmlrpcConfgDb.setServerURL(new URL(<br /> "http://localhost:8069/xmlrpc/object"));<br /><br /> xmlrpc.setConfig(xmlrpcConfgDb);<br /><br /> String dbname = "openerp";<br /> int uid = 1;<br /> String pwd = "000000";<br /> String model = "x_test";<br /><br /> Object[] params = new Object[] { "openerp", 1, "000000", "x_test",<br /> "read", ids, fields };<br /><br /> try {<br /><br /> Object[] o = (Object[]) xmlrpc.execute("execute", params);<br /><br /> System.out.println("finished");<br /> // System.out.println(o.length);<br /> for (Object obj : o) {<br /> System.out.println(obj);<br /> }<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /><br /> }<br /> }<br /><br /> public static void readView() throws MalformedURLException {<br /> Object[] ids = { 1276 };<br /> Object[] fields = { "arch" };<br /><br /> XmlRpcClient xmlrpc = new XmlRpcClient();<br /><br /> XmlRpcClientConfigImpl xmlrpcConfgDb = new XmlRpcClientConfigImpl();<br /> xmlrpcConfgDb.setEnabledForExceptions(true);<br /> xmlrpcConfgDb.setServerURL(new URL(<br /> "http://localhost:8069/xmlrpc/object"));<br /><br /> xmlrpc.setConfig(xmlrpcConfgDb);<br /><br /> String dbname = "openerp";<br /> int uid = 1;<br /> String pwd = "000000";<br /> String model = "x_test";<br /><br /> Object[] params = new Object[] { "openerp", 1, "000000", "ir.ui.view",<br /> "read", ids, fields };<br /><br /> try {<br /><br /> Object[] o = (Object[]) xmlrpc.execute("execute", params);<br /><br /> // System.out.println("finished");<br /> System.out.println(o.length);<br /> for (Object obj : o) {<br /> // System.out.println(obj);<br /><br /> HashMap h = (HashMap)obj;<br /> //System.out.println(h.keySet().size());<br /> System.out.println("the id is:");<br /> System.out.println(h.get("id"));<br /> System.out.println("the arch is:");<br /> System.out.println(h.get("arch"));<br /> }<br /> } catch (Exception e) {<br /> e.printStackTrace();<br /><br /> }<br /> }<br /><br />}<br />
i'm '深圳-steven' ... -
坐自己沙发...
广告招租 ;D