2017-02-12 55 views
1

我嘗試這樣的代碼來創建XML文件創建一個從控制器輸出XML類型笨

public function get_markers() 
{ 
    $this->load->dbutil(); 
    $sql = "select name, address, lat, lng, type from restaurant where status=1"; 
    $query = $this->db->query($sql); 
    $config = array (
     'root' => 'markers', 
     'element' => 'marker', 
     'newline' => "\n", 
     'tab'  => "\t" 
    ); 
    $xml = $this->dbutil->xml_from_result($query, $config); 
    $this->output->set_content_type('text/xml'); 
    $this->output->set_output($xml); 
} 

此代碼返回下面的結果:

<markers> 
    <marker> 
     <name>Soto Bangkong</name> 
     <address>JL. Setiabudi No. 229 Srondol, Srondol Kulon</address> 
     <lat>-7.06223759219975</lat> 
     <lng>110.4129814497071</lng> 
     <type>7</type> 
    </marker> 
    <marker> 
     <name>Waroeng Semawis</name> 
     <address>Jalan Gang Warung No. 50, Kauman</address> 
     <lat>-6.974595476216744</lat> 
     <lng>110.42663989422613</lng> 
     <type>7</type> 
    </marker> 
</markers> 

如何從codeignite控制器使XML文件 r,在節點內添加屬性。要完全一樣:如果你想寫csv文件是一件容易的事

<markers> 
<marker id="1" name="Billy Kwong" address="1/28 Macleay Street, Elizabeth Bay, NSW" lat="-33.869843" lng="-151.225769" type="restaurant"/> 
<marker id="2" name="Love.Fish" address="580 Darling Street, Rozelle, NSW" lat="-33.861034" lng="151.171936" type="restaurant"/> 
</markers> 

回答

0

試試這個

function get_report(){ 
    $this->load->model('my_model'); 
    $this->load->dbutil(); 
    $this->load->helper('file'); 
    /* get the object */ 
    $report = $this->my_model->index(); 
    /* pass it to db utility function */ 
    $new_report = $this->dbutil->xml_from_result($report); 
    /* Now use it to write file. write_file helper function will do it */ 
    write_file('xml_file.xml',$new_report); 
    /* Done */ 
} 

。 只需使用csv_from_result()方法並使用write_file('csv_file.csv,$ new_report)。