怎么将json存储转成xml数据格式存储

怎么将下面的json存储转成xml数据格式存储


 private String generateTableInfo(Uri uri, String where, String filename, boolean needIds) {
        JSONArray jAResult = new JSONArray();
        String ids = "";
        Cursor c = mContext.getContentResolver().query(uri, null, where, null, null);
        if (c != null) {
            try {
                if (c.moveToFirst()) {
                    for (int i = 0; i < c.getCount(); i++) {
                        c.moveToPosition(i);
                        JSONObject jsonO = new JSONObject();
                        int ColumnCount = c.getColumnCount();
                        for (int j=0; j<ColumnCount; j++) {
                            if (uri.equals(CaseInfo.CONTENT_URI) && c.getColumnName(j).equals(CaseInfo._PATH)) {
                                String filepath = c.getString(j);
                                File file = new File(filepath);
                                zipFilelist.add(file);
                                jsonO.put(c.getColumnName(j), file.getName());
                            } else {
                                jsonO.put(c.getColumnName(j), c.getString(j));
                            }
                            if (needIds && uri.equals(AttachInfo.CONTENT_URI) && c.getColumnName(j).equals(AttachInfo._BYTEID)) {
                                if (ids.length() == 0) {
                                    ids = "(\'" + c.getString(j);
                                } else {
                                    ids += "\',\'" + c.getString(j);
                                }
                            }
                        }
                        jAResult.put(jsonO);
                    }
                    if (needIds && uri.equals(AttachInfo.CONTENT_URI)) {
                        if (ids.length() != 0) {
                            ids += "\')";
                        }
                    }
                }
            } catch (Exception e) {
            //  TODO: handle exception
                Log.i(e.getMessage());
            } finally {
                c.close();
            }
        }

Document do=DocumentHelper,createDocument();
do.setXMLEncoding("UTF-8");
do.setName("?XML");
Element root=do.addElement("root");//自己定义
Element user=root.addElement("userid");//自己定义
user.addAttribute("key",value);
//根据这种格式 把json转换成list数据后遍历进去


获得json里面的所有key和value(类似map形式),value里面也有可能有对象。
所以,写一个添加xml Element的方法,递归添加到xml中。
方法告诉你了,具体实施自己写。

首先有个疑问,为什么不能直接存json呢?

如果要转xml,还是使用第三方包吧,省很多工作量。

可以尝试下gson,goolge的json转换的类

比如可按下面这样转,具体接口可查看gson里面

    public static String toXML(String jsonString, String root, String element)
    {
        JSON json = JSONSerializer.toJSON(jsonString);
        XMLSerializer xmlSerializer = new XMLSerializer();
        xmlSerializer.setRootName(root);
        xmlSerializer.setElementName(element);
        xmlSerializer.setTypeHintsEnabled(false);
        String xml = xmlSerializer.write(json);
        return xml;
    }

得到你的json值,组装成Map或者List 然后遍历添加到xml Element的方法,没有多大麻烦的