How do I view an XML file in browser using Codeigniter
Never mind, I figured it out. Instead of using CodeIgniter's $this->load->view(), I just load the file and echoed it out to the screen.
public function view_xml(){ header("Content-type: text/xml"); $xml_file = file_get_contents($_SERVER['DOCUMENT_ROOT'] . "/xml/example.xml"); echo $xml_file;}
You cannot view directly the .xml file, you need to prase it is a database file just like JSON.
Below is an .xml file:
<?xml version="1.0" encoding="ISO-8859-1"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>
We want to output the element names and data from the XML file above.Here's what to do:
- Load the XML file
- Get the name of the first element
- Create a loop that will trigger on each child node, using thechildren() function
- Output the element name and data for each child node
Example:
<?php$xml = simplexml_load_file("test.xml");echo $xml->getName() . "<br />";foreach($xml->children() as $child) { echo $child->getName() . ": " . $child . "<br />"; }?>
The result:
noteto: Tovefrom: Janiheading: Reminderbody: Don't forget me this weekend!