4

Ok, I have looked around, and I can't seem to figure this out.

Here is my CodeIgniter Structure:

  • orders
    • application
      • controller
      • model
      • view
    • xml

As you can see, the XML folder is outside the application folder. Inside the XML folder I have an xml file named example.xml. Inside the controller, how do I load the xml file to view it in the browser?

class Example extends CI_Controller {
   public function view_xml(){
       header("Content-type: text/xml");
       $this->load->view() ??????? // Here is where I'm stuck
   }
}

I have tried using BASEPATH and $_SERVER['DOCUMENT_ROOT'] but with no luck. I want to be able to type in the url example/view_xml to view the xml in the browser.

2 Answers 2

3

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;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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:

  1. Load the XML file
  2. Get the name of the first element
  3. Create a loop that will trigger on each child node, using the children() function
  4. 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:

note
to: Tove
from: Jani
heading: Reminder
body: Don't forget me this weekend!

2 Comments

Really, there is no way for me to view an XML in the browser? I'm not looking to parse the XML, but I need to show the tags as well. I mean, I can do something like this $output = "<root><name>sample_name</name></root>"; print($output); and it is fine, but loading a file with XML data isn't working at all.
you want to open the xml just the text contents of it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.