0

I want to create an element in XML file using CodeIgniter, but every time I'm getting a fatal error: "Call to undefined method SimpleXMLElement::createElement()". Here is my code in Controller, View & Model class, what am I doing wrong?

Controller (xml_insert.php):

    <?php

class Xml_insert extends CI_Controller {

function index() {

    $this->load->model('xml_insert_model');
    $data['rows'] = $this->xml_insert_model->getAll();
    $this->load->view('xml_insert_view', $data);
}

function insert() {
    $this->load->model('xml_insert_model');
    $data['rows'] = $this->xml_insert_model->getAll();

    foreach ($data['rows'] as $r) {
        $path1 = $r->xml_file_path;
        $xml = simplexml_load_file($path1);

        $newAct = $_POST['activity'];

        $root = $xml->firstChild;

        $newElement = $xml->createElement('activity');
        $root->appendChild($newElement);
        $newText = $xml->createTextNode($newAct);
        $newElement->appendChild($newText);

        $xml->save('$path1');
        $this->index();
    }
}

}

View (xml_insert_view.php):

    <!doctype html>
    <html lang="en">
    <head>
 <meta charset="UTF-8">
 <title>Document</title>
    </head>
    <body>
   <?php foreach ($rows as $r): ?>
    <?php
            $path1 = $r->xml_file_path;
            $xml = simplexml_load_file($path1);
    ?>
    <?php  foreach ($xml->children() as $activity) : ?>
    <?php echo "Activity : " . $activity . " <br />"; ?>
    <?php endforeach; ?>
      <?php endforeach; ?>

      <form name="input" action="index.php/xml_insert/insert" method="post">
      insert activity:
      <input type="text" name="activity"/>
      <input type="submit" value="send"/>
      </form>
  </body>
</html>

Model (xml_insert_model.php):

    <?php
    class Xml_insert_model extends CI_Model
    {
      function getAll()
      {

         $q = $this->db->get("XML");

         if ($q->num_rows > 0) {

           foreach ($q->result() as $row) {
              $data[] = $row;
           }

          return $data;
         }
      }
   }

XML file(sample.xml)

    <?xml version="1.0"?>
    <list>
      <activity>swimming</activity>
      <activity>running</activity>
      <activity>Jogging</activity>
      <activity>Theatre</activity>
      <activity>Programming</activity>
  <activity>driving</activity>
  <activity>eating</activity>
    </list>
1
  • You'll want to define your specific issue further. Pasting all code won't get you much support. And my first thought, based on intro and no need to look at code, is Are you sure that function createElement is available to you? Commented Feb 19, 2014 at 16:25

1 Answer 1

1

I think you are trying to use functions of DomDocument

instead of

$xml = simplexml_load_file($path1);

Try

$xml = new DomDocument();
$xml->load($path1);

You can use addChild method of simpleXML if you prefer.

Sign up to request clarification or add additional context in comments.

Comments

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.