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