2013-05-01 43 views
1

人,EasyUI - XML數據加載到數據網格

這是張貼在http://www.jeasyui.com/forum/index.php?topic=1144問題的延續。

我想加載一個XML數據(雖然默認情況下easyui消費JSON)。論壇中的例子展示瞭如何加載xml,如果它是按原樣寫入的,但不是鏈接xml文件或URL的方式。

從本質上講,這個問題是

如何將XML文件加載到一個easyUI DataGrid中

目前 「數據」 被指定爲

data: '<root><people><name>name1</name><address>address1</address></people><people><name>name2</name><address>address2</address></people></root>', 

但是,它可能有數據一個文件或URL

data: '/some/url/input.xml' 

以下全部示例。

<table class="easyui-datagrid" title="Load XML Data" style="width:300px;height:200px"  
      data-options=" 
      **data: '/some/url/input.xml',**    
      loadFilter: function(xml){ 
        var rows = []; 
        $(xml).find('people').each(function(){ 
         var p = $(this); 
         var row = { 
          name: p.find('name').text(), 
          address: p.find('address').text() 
         }; 
         rows.push(row); 
        }); 
        return {total:rows.length,rows:rows}; 
       }  
    "> 
    <thead>   
     <tr>    
      <th data-options="field:'name'">Name</th>   
      <th data-options="field:'address'">Address</th>   
     </tr> 
    </thead> 
</table> 

在此先感謝。

+0

你有沒有找到解決方案? – donlaur 2013-11-26 18:09:04

+0

我沒有在easyui中找到。但我在後端編寫了一個xml-> json解析器 – diaryfolio 2013-12-10 15:49:14

+0

這是否解決了您的問題?它對我很好。我想,對你來說太遲了,但它可能會幫助其他人。 – donlaur 2013-12-10 16:09:41

回答

1

我能解決這個問題。它需要一些工作和一些從一種格式到另一種格式的數據來回移動。我從CRUD數據網格教程版本開始,所以我的答案可能有點不同。我從CRUD示例開始,從mySQL解決方案轉換爲使用XML。
http://www.jeasyui.com/tutorial/app/crud2.php

$(function(){ 
     $('#dg').edatagrid({ 
      url: 'get_data.php' /* pulls in the data from a xml file */ 
     }); 
    }); 

我的例子其實是有讀(URL),創造(saveURL),更新(器updateURL)和刪除(destroyUrl),但對於這個答案,我只是把URL中讀取數據。

對於我的get_data.php我將我的XML文件轉換爲JSON。我還刪除了該XML文件中的根。

if(!$xml = simplexml_load_file("mydata.xml", null, LIBXML_NOCDATA)) { 
echo "unable to load file"; 
} 
{ 
$xmlarray = array(); // create a new array 
    foreach ($xml as $element) { // loop through the xml file elements 
     array_push($xmlarray, $element); // push the elements on the xml array 
    } 
} 

echo json_encode($xmlarray); // encode that json without the root xml element 

我仍然堅持使用simplexml,但在進行刪除和更新時有更簡單的方法。你可以使用DOM或其​​他手段。