在Jqgrid中的一列中显示xml数据

时间:2013-04-01 20:25:21

标签: jquery jqgrid

我是jqgrid的新手,并且要求我在哪里加载xml数据并在浏览器上显示。我有下面的对象

          Object
           id
           xmldata
           attribute1
           attribute2
           attribute3
           attribute4

我的网页上有json

        jq("#grid").jqGrid({
        url:'/url',
        datatype: 'json',
        mtype: 'GET',
        colNames:['Id', 'xml data', 'attribute1', 'attribute2', 'attribute3', 'attribute4'],
        colModel:[
            {name:'id',index:'id', width:55,editable:false,editoptions:{readonly:true,size:10},hidden:true},
            {name:'XMLData',index:'xmldata', width:100,editable:true, editrules:{required:true}, editoptions:{size:10}},
            {name:'attribute1',index:'attribute1', width:100,editable:true, editrules:{required:true}, editoptions:{size:10}},
            {name:'attribute2',index:'attribute2', width:100,editable:true, editrules:{required:true}, editoptions:{size:10}},
            {name:'attribute3',index:'attribute3', width:100,editable:true, editrules:{required:true}, editoptions:{size:10}},
            {name:'attribute4',index:'attribute4', width:100,editable:true, editrules:{required:true}, editoptions:{size:10}},

        ],

但xmldata属性未加载,其他数据已加载到页面上。我知道请求体是类型json而不是xml。但我有混音器json和xml数据。如何使用json类型显示xml数据。还是我们有任何警觉性的。我需要将一个列的xml属性显示在另一个下面。

由于

1 个答案:

答案 0 :(得分:0)

可以使用不同的方法。但他们处理的是数据预处理。


1 org.json命名空间中的XML class为您提供此功能。

您必须调用静态toJSONObject方法:

将格式良好(但不一定有效)的XML字符串转换为JSONObject。由于JSON是数据格式而XML是文档格式,因此在此转换中可能会丢失某些信息。 XML使用元素,属性和内容文本,而JSON使用无序的名称/值对集合和值数组。 JSON不喜欢区分元素和属性。类似元素的序列表示为JSONArrays。内容文本可以放在“内容”成员中。评论,序言,DTD和被忽略。

这会将xml转换为JSON。接下来,可以将此数据附加到对象并根据需要使用。

2. 另一种方法是使用函数xml2json(xml,tab)在jQuery中。有关更多说明,您可以转到此link。这可以在customformatter列中使用。