2012-04-28 12 views
7

का उपयोग कर निर्माण मैं Codeigniter में इस कोड का उपयोग कर रहा एक्सएमएल उत्पन्न करने के लिए:एक्सएमएल CodeIgniter

public function get_cuisine() 
{ 
    $this->load->dbutil(); 
    $sql = "select * from cuisine"; 
    $query = $this->db->query($sql); 
    $config = array (
     'root' => 'root', 
     'element' => 'element', 
     'newline' => "\n", 
     'tab'  => "\t" 
    ); 
    echo $this->dbutil->xml_from_result($query, $config); 
} 

लेकिन यह सामान्य प्रिंट प्रारूप को दर्शाता है। मैं इसे XML प्रकार पृष्ठ के रूप में दिखाने के लिए कैसे प्राप्त कर सकता हूं?

उत्तर

15

आप एक्सएमएल हेडर सेट करना होगा अगर तुम उत्पादन करने के लिए सीधे फ़ाइल हैं:

Codeigniter Output class का उपयोग करना:

$xml = $this->dbutil->xml_from_result($query, $config); 
$this->output->set_content_type('text/xml'); 
$this->output->set_output($xml); 

या आप सादे पीएचपी का उपयोग headers सेट करने के लिए कर सकते हैं:

header('Content-type: text/xml'); 
echo $this->dbutil->xml_from_result($query, $config); 

या आप सीआई download helper उपयोग कर सकते हैं:

$xml = $this->dbutil->xml_from_result($query, $config); 
$this->load->helper('download'); 
force_download('myfile.xml', $xml); 

या यह file helper के साथ एक फ़ाइल पर लिखें:

$xml = $this->dbutil->xml_from_result($query, $config); 
$this->load->helper('file'); 
$file_name = '/path/to/myfile.xml'; 
write_file($file_name, $xml); 
// Optionally redirect to the file you (hopefully) just created 
redirect($file_name); 
+0

यो सका कृपया $ file_name = '/path/to/myfile.xml' के बारे में थोड़ा विस्तृत करें; मेरा फ़ाइल पथ $ file_name = base_url ('एप्लिकेशन/नियंत्रक/test.xml') है; मुझे लगता है कि समस्या $ file_name में है। – Shiplu

+0

@Shiplu फ़ाइल के पूर्ण सर्वर पथ का उपयोग करें, न कि आपकी वेबसाइट का यूआरएल। –

2

मैं भी यही सवाल किया था। मैंने इसे गुगल किया है। यह समाधान मिला। और यह मेरे लिए पूरी तरह से काम करता है। Click here to get the source code

बस डाउनलोड करने और अनज़िप (यह निकालने)

तो आवेदन के में xml_writer.php कॉपी> अपने पुस्तकालयों के लिए निकाले फ़ोल्डर की लाइब्रेरी को अपने Codeigniter परियोजना में फ़ोल्डर।

इसके अलावा आवेदन के में xml.php कॉपी> अपने नियंत्रकों फ़ोल्डर

करने में नियंत्रक अंत में xml.php निकाले फ़ोल्डर के दृश्यों का आनंद आपके विचार करने के लिए कॉपी करें और चलाते हैं। ।

यह है कि ...

0

कस्टम समाधान:

$mysql_data = $this->db->get('products') 
        ->result_array(); 
$xml = '<root>'; 
foreach($mysql_data as $row){ 
    $xml .= '<item> 
      <name>'.$row['title'].'</name> 
      <price>'.$row['price'].'</price> 
      <image>'.$row['pic'].'</image> 
      </item>'; 
} 
$xml .= '</root>'; 
$this->output->set_content_type('text/xml'); 
$this->output->set_output($xml);