我如何解析 xmlrpc 响应(Odoo API 读取),使用 Java

How can i parse xmlrpc respnse (Odoo API read), using Java

Odoo 新手,我必须使用 Odoo API 的读取方法获取产品相关的类别。我得到一个对象列表,然后对于每个类别我需要提取字段 product_tmpl_ids,一个整数列表。我不知道该怎么做。这是方法:

public List readModelfields(String modelName, List<Integer>ids, List<String> fields, Integer uid)throws XmlRpcException {
    List record = (List)Arrays.asList((Object[])models.execute("execute_kw", Arrays.asList(
            db, uid, password,
            modelName, "read",
             ids,
            new HashMap() {{
                put("fields", fields);
            }}
        )));
    return record;
}

这是剩余的代码:

List<String> fields = new ArrayList<>();
fields.add("product_tmpl_ids");
List categoryIds = (List<Integer>)service.searchByStrParameter("product.public.category", "name", "A - Administration / Office", uid);
List result = (List)service.readModelfields("product.public.category", categoryIds, fields, uid);

有人可以帮忙吗?如何从获取的对象中提取字段?

您将搜索方法的结果(一个列表)传递给 readModelfields,它按原样用于调用 read 方法,如外部 API [=19 中所述=] 方法 ids 必须在列表中传递:

Arrays.asList(ids),

您在同一型号上使用了搜索和读取方法,您可以改用search_read

以下示例显示如何获取 res.partners

的 many2many 字段 category_id
public List searchReadModelfields(String modelName, List domain, List<String> fields, Integer uid) throws XmlRpcException {
    return (List)Arrays.asList((Object[])models.execute("execute_kw", Arrays.asList(
            db, uid, password,
            modelName,
            "search_read",
            Arrays.asList(domain),
            new HashMap() {{
                put("fields", fields);
            }}
    )));
}

调用 searchReadModelfields 并打印出值:

List<String> fields = new ArrayList<>();
fields.add("category_id");
List domain = Arrays.asList(
        Arrays.asList("category_id", "!=", false)
);

// return list of hashmaps)
List result = (List)service.searchReadModelfields("res.partner", domain, fields, uid);

HashMap values;
Integer partner_id;
Object[] category_ids;
for(Object partner_data: result) {
    values  = (HashMap)partner_data;
    partner_id = (Integer) values.get("id");
    category_ids = (Object [])values.get("category_id");

    System.out.printf("{partner_id: %s, ", partner_id);
    System.out.print("category_ids:[");
    for(Object category_id: category_ids) {
        System.out.printf("%s,",
                category_id
        );
    }
    System.out.println("]}");
 }