Submitting a PHP form to store data as XML in a file












0














I have set up this form with the intention of when I add details into the; title, description and emotion text fields and then press submit, this data will be stored as xml in the php file.



I am struggling to set up the controller so that when I submit the form it actually stores this data. The example that I will provide below worked previously when I was storing it on my database, but I am now wanting to store it as xml.




Projects Controller




namespace AppHttpControllers;

use IlluminateHttpRequest;

use OrchestraParserXmlFacade as XmlParser;

interface OutputType
{
public function generate();
}

//project class has been imported
use AppProject;

class ProjectsController extends Controller
{
public function index()
{
//because the class has been imported we can reference it like this rather than 'AppProject::all();'
$projects = Project::all();



//passing the JSON to the view

return view ('projects.index', ['projects'=> $projects]); //can also use 'compact('projects') instead of the ['projects'=> $projects]
}



//new method called create

public function create()
{

return view ('projects.create');

}

//new method called store


public function store()
{

if (isset($_POST['submit'])) {
$xmlType = new XMLType();
$xmlType->createParent('programme', ['id' => 1])
->addElement('name', $_POST['name'], ['type' => 'string'])
->addElement('description', $_POST['description'])
->addElement('mood', $_POST['mood'])
->groupParentAndElements()
->createParent('others', ['id' => 2, 'attr' => 'ex'])
->addElement('example', 'A value here')
->groupParentAndElements();

//whenever you wish to output
$xmlType->generate();
}



return view('projects.upload');
}


//changed this from upload to show
public function upload()
{


return view('projects.upload');
}

//changed this from upload to show
public function show()
{


return view ('projects.upload', compact('user'));
}

public function slider()
{


return view ('projects.upload', compact('user'));
}


}



Database.php (where I want to store submitted data as xml




<?
$xmlstr = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<programmeList>
<programme id="1">
<name>School of Rock</name>
<image>images/schoolofrock.jpg</image>
<description>After being kicked out of a rock band, Dewey Finn becomes a substitute teacher </description>
<mood>Agitated</mood>
</programme>
<programme id="2">
<name>Pitch Perfect 2</name>
<image>images/pitchperfect2.jpg</image>
<description>After a humiliating command performance at The Kennedy Center, the Barden Bellas enter an international competition that no American group has ever won in order to regain their status and right to perform.</description>
<mood>Fearless</mood>
</programme>
</programmeList>
XML;
?>



Create.php (view)




 <link rel="stylesheet" href="CSS/app.css" type="text/css">
<meta name = "viewport" content="width = device-width, initial-scale = 1">
<link rel = "stylesheet" href = "https://www.w3schools.com. /lib/w3.css">
<!DOCTYPE html>
<html>
<head>
<title>Upload</title>
</head>
<body>
<!-- Sky logo + Sky cinema logo -->
<div class="box">
<td style="text-align:center;color:black;font-size:50px;">
<img src="/images/sky.png" title="Sky" alt="Sky" width="auto" height="125" />
</td>
<td style="text-align: right;position: relative; color:black;font-size:50px;">
<img src="/images/sky_cinema.png" title="sky_cinema" alt="Sky" width="auto" height="125" />
</td>
</div>

<div>
<ul class="w3-navbar w3-black">
<li><a href="/projects/upload">Moodslider Home</a></li>
<li><a href="/projects/create">Upload Content</a></li>
</ul>
</div>


<div>
<h1>Upload a movie</h1>
</div>
<!--post request followed by an action which states where the data will be posted/returned -->

<form method="POST" action="/projects">


<form action="/projects/upload" method="post" enctype="multipart/form-data">
{{ @csrf_field() }}
<h4>Movie Title</h4><input type="text" name="name">
<br/>
<h4>Movie Description</h4><textarea name="description" rows="5" cols="50" wrap="physical">
</textarea>
<h4>Movie Emotion</h4>
<input type="radio" name="mood" value="Agitated">Agitated<br>
<input type="radio" name="mood" value="Calm">Calm<br>
<input type="radio" name="mood" value="Happy">Happy<br>
<input type="radio" name="mood" value="Sad">Sad<br>
<input type="radio" name="mood" value="Tired">Tired<br>
<input type="radio" name="mood" value="WideAwake">Wide Awake<br>
<input type="radio" name="mood" value="Scared">Scared<br>
<input type="radio" name="mood" value="Fearless">Fearless<br>
<br/>
Choose a file to upload: <input type="file" name="fileToUpload" id="fileToUpload" />
<br/>
<br/>
<input type="submit" value="submit" name="submit" />
</form>
</form>

</body>
</html>



XMLType




<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

//import the interface above

class XMLType implements OutputType
{
protected $xml;
protected $parentElement;
protected $elementGroup;

public function __construct()
{
$this->xml= new DOMDocument('1.0', 'ISO-8859-15');
}

public function createParent($type, $attributes = )
{
$this->parentElement = $this->xml->createElement($type);
$this->addAttributes($this->parentElement, $attributes);
return $this;
}

private function addAttributes(&$element, $attributes)
{
if (sizeof($attributes) > 0) {
foreach($attributes as $type => $value) {
$element->setAttribute($type, $value);
}
}
}

public function addElement($type, $element, $attributes = )
{
$this->elementGroup = $this->xml->createElement($type, $element);
$this->addAttributes($this->elementGroup, $attributes);
return $this;
}

public function groupParentAndElements()
{
$this->parentElement->appendChild($this->elementGroup);
return $this;
}

public function generate()
{
$this->xml->appendChild($this->parentElement);
print $this->xml->saveXML(); //here I am saving and printing but you can change to suit your needs. It is at this point it is ready to generate the XML
}

}









share|improve this question





























    0














    I have set up this form with the intention of when I add details into the; title, description and emotion text fields and then press submit, this data will be stored as xml in the php file.



    I am struggling to set up the controller so that when I submit the form it actually stores this data. The example that I will provide below worked previously when I was storing it on my database, but I am now wanting to store it as xml.




    Projects Controller




    namespace AppHttpControllers;

    use IlluminateHttpRequest;

    use OrchestraParserXmlFacade as XmlParser;

    interface OutputType
    {
    public function generate();
    }

    //project class has been imported
    use AppProject;

    class ProjectsController extends Controller
    {
    public function index()
    {
    //because the class has been imported we can reference it like this rather than 'AppProject::all();'
    $projects = Project::all();



    //passing the JSON to the view

    return view ('projects.index', ['projects'=> $projects]); //can also use 'compact('projects') instead of the ['projects'=> $projects]
    }



    //new method called create

    public function create()
    {

    return view ('projects.create');

    }

    //new method called store


    public function store()
    {

    if (isset($_POST['submit'])) {
    $xmlType = new XMLType();
    $xmlType->createParent('programme', ['id' => 1])
    ->addElement('name', $_POST['name'], ['type' => 'string'])
    ->addElement('description', $_POST['description'])
    ->addElement('mood', $_POST['mood'])
    ->groupParentAndElements()
    ->createParent('others', ['id' => 2, 'attr' => 'ex'])
    ->addElement('example', 'A value here')
    ->groupParentAndElements();

    //whenever you wish to output
    $xmlType->generate();
    }



    return view('projects.upload');
    }


    //changed this from upload to show
    public function upload()
    {


    return view('projects.upload');
    }

    //changed this from upload to show
    public function show()
    {


    return view ('projects.upload', compact('user'));
    }

    public function slider()
    {


    return view ('projects.upload', compact('user'));
    }


    }



    Database.php (where I want to store submitted data as xml




    <?
    $xmlstr = <<<XML
    <?xml version="1.0" encoding="UTF-8"?>
    <programmeList>
    <programme id="1">
    <name>School of Rock</name>
    <image>images/schoolofrock.jpg</image>
    <description>After being kicked out of a rock band, Dewey Finn becomes a substitute teacher </description>
    <mood>Agitated</mood>
    </programme>
    <programme id="2">
    <name>Pitch Perfect 2</name>
    <image>images/pitchperfect2.jpg</image>
    <description>After a humiliating command performance at The Kennedy Center, the Barden Bellas enter an international competition that no American group has ever won in order to regain their status and right to perform.</description>
    <mood>Fearless</mood>
    </programme>
    </programmeList>
    XML;
    ?>



    Create.php (view)




     <link rel="stylesheet" href="CSS/app.css" type="text/css">
    <meta name = "viewport" content="width = device-width, initial-scale = 1">
    <link rel = "stylesheet" href = "https://www.w3schools.com. /lib/w3.css">
    <!DOCTYPE html>
    <html>
    <head>
    <title>Upload</title>
    </head>
    <body>
    <!-- Sky logo + Sky cinema logo -->
    <div class="box">
    <td style="text-align:center;color:black;font-size:50px;">
    <img src="/images/sky.png" title="Sky" alt="Sky" width="auto" height="125" />
    </td>
    <td style="text-align: right;position: relative; color:black;font-size:50px;">
    <img src="/images/sky_cinema.png" title="sky_cinema" alt="Sky" width="auto" height="125" />
    </td>
    </div>

    <div>
    <ul class="w3-navbar w3-black">
    <li><a href="/projects/upload">Moodslider Home</a></li>
    <li><a href="/projects/create">Upload Content</a></li>
    </ul>
    </div>


    <div>
    <h1>Upload a movie</h1>
    </div>
    <!--post request followed by an action which states where the data will be posted/returned -->

    <form method="POST" action="/projects">


    <form action="/projects/upload" method="post" enctype="multipart/form-data">
    {{ @csrf_field() }}
    <h4>Movie Title</h4><input type="text" name="name">
    <br/>
    <h4>Movie Description</h4><textarea name="description" rows="5" cols="50" wrap="physical">
    </textarea>
    <h4>Movie Emotion</h4>
    <input type="radio" name="mood" value="Agitated">Agitated<br>
    <input type="radio" name="mood" value="Calm">Calm<br>
    <input type="radio" name="mood" value="Happy">Happy<br>
    <input type="radio" name="mood" value="Sad">Sad<br>
    <input type="radio" name="mood" value="Tired">Tired<br>
    <input type="radio" name="mood" value="WideAwake">Wide Awake<br>
    <input type="radio" name="mood" value="Scared">Scared<br>
    <input type="radio" name="mood" value="Fearless">Fearless<br>
    <br/>
    Choose a file to upload: <input type="file" name="fileToUpload" id="fileToUpload" />
    <br/>
    <br/>
    <input type="submit" value="submit" name="submit" />
    </form>
    </form>

    </body>
    </html>



    XMLType




    <?php

    namespace AppHttpControllers;

    use IlluminateHttpRequest;

    //import the interface above

    class XMLType implements OutputType
    {
    protected $xml;
    protected $parentElement;
    protected $elementGroup;

    public function __construct()
    {
    $this->xml= new DOMDocument('1.0', 'ISO-8859-15');
    }

    public function createParent($type, $attributes = )
    {
    $this->parentElement = $this->xml->createElement($type);
    $this->addAttributes($this->parentElement, $attributes);
    return $this;
    }

    private function addAttributes(&$element, $attributes)
    {
    if (sizeof($attributes) > 0) {
    foreach($attributes as $type => $value) {
    $element->setAttribute($type, $value);
    }
    }
    }

    public function addElement($type, $element, $attributes = )
    {
    $this->elementGroup = $this->xml->createElement($type, $element);
    $this->addAttributes($this->elementGroup, $attributes);
    return $this;
    }

    public function groupParentAndElements()
    {
    $this->parentElement->appendChild($this->elementGroup);
    return $this;
    }

    public function generate()
    {
    $this->xml->appendChild($this->parentElement);
    print $this->xml->saveXML(); //here I am saving and printing but you can change to suit your needs. It is at this point it is ready to generate the XML
    }

    }









    share|improve this question



























      0












      0








      0







      I have set up this form with the intention of when I add details into the; title, description and emotion text fields and then press submit, this data will be stored as xml in the php file.



      I am struggling to set up the controller so that when I submit the form it actually stores this data. The example that I will provide below worked previously when I was storing it on my database, but I am now wanting to store it as xml.




      Projects Controller




      namespace AppHttpControllers;

      use IlluminateHttpRequest;

      use OrchestraParserXmlFacade as XmlParser;

      interface OutputType
      {
      public function generate();
      }

      //project class has been imported
      use AppProject;

      class ProjectsController extends Controller
      {
      public function index()
      {
      //because the class has been imported we can reference it like this rather than 'AppProject::all();'
      $projects = Project::all();



      //passing the JSON to the view

      return view ('projects.index', ['projects'=> $projects]); //can also use 'compact('projects') instead of the ['projects'=> $projects]
      }



      //new method called create

      public function create()
      {

      return view ('projects.create');

      }

      //new method called store


      public function store()
      {

      if (isset($_POST['submit'])) {
      $xmlType = new XMLType();
      $xmlType->createParent('programme', ['id' => 1])
      ->addElement('name', $_POST['name'], ['type' => 'string'])
      ->addElement('description', $_POST['description'])
      ->addElement('mood', $_POST['mood'])
      ->groupParentAndElements()
      ->createParent('others', ['id' => 2, 'attr' => 'ex'])
      ->addElement('example', 'A value here')
      ->groupParentAndElements();

      //whenever you wish to output
      $xmlType->generate();
      }



      return view('projects.upload');
      }


      //changed this from upload to show
      public function upload()
      {


      return view('projects.upload');
      }

      //changed this from upload to show
      public function show()
      {


      return view ('projects.upload', compact('user'));
      }

      public function slider()
      {


      return view ('projects.upload', compact('user'));
      }


      }



      Database.php (where I want to store submitted data as xml




      <?
      $xmlstr = <<<XML
      <?xml version="1.0" encoding="UTF-8"?>
      <programmeList>
      <programme id="1">
      <name>School of Rock</name>
      <image>images/schoolofrock.jpg</image>
      <description>After being kicked out of a rock band, Dewey Finn becomes a substitute teacher </description>
      <mood>Agitated</mood>
      </programme>
      <programme id="2">
      <name>Pitch Perfect 2</name>
      <image>images/pitchperfect2.jpg</image>
      <description>After a humiliating command performance at The Kennedy Center, the Barden Bellas enter an international competition that no American group has ever won in order to regain their status and right to perform.</description>
      <mood>Fearless</mood>
      </programme>
      </programmeList>
      XML;
      ?>



      Create.php (view)




       <link rel="stylesheet" href="CSS/app.css" type="text/css">
      <meta name = "viewport" content="width = device-width, initial-scale = 1">
      <link rel = "stylesheet" href = "https://www.w3schools.com. /lib/w3.css">
      <!DOCTYPE html>
      <html>
      <head>
      <title>Upload</title>
      </head>
      <body>
      <!-- Sky logo + Sky cinema logo -->
      <div class="box">
      <td style="text-align:center;color:black;font-size:50px;">
      <img src="/images/sky.png" title="Sky" alt="Sky" width="auto" height="125" />
      </td>
      <td style="text-align: right;position: relative; color:black;font-size:50px;">
      <img src="/images/sky_cinema.png" title="sky_cinema" alt="Sky" width="auto" height="125" />
      </td>
      </div>

      <div>
      <ul class="w3-navbar w3-black">
      <li><a href="/projects/upload">Moodslider Home</a></li>
      <li><a href="/projects/create">Upload Content</a></li>
      </ul>
      </div>


      <div>
      <h1>Upload a movie</h1>
      </div>
      <!--post request followed by an action which states where the data will be posted/returned -->

      <form method="POST" action="/projects">


      <form action="/projects/upload" method="post" enctype="multipart/form-data">
      {{ @csrf_field() }}
      <h4>Movie Title</h4><input type="text" name="name">
      <br/>
      <h4>Movie Description</h4><textarea name="description" rows="5" cols="50" wrap="physical">
      </textarea>
      <h4>Movie Emotion</h4>
      <input type="radio" name="mood" value="Agitated">Agitated<br>
      <input type="radio" name="mood" value="Calm">Calm<br>
      <input type="radio" name="mood" value="Happy">Happy<br>
      <input type="radio" name="mood" value="Sad">Sad<br>
      <input type="radio" name="mood" value="Tired">Tired<br>
      <input type="radio" name="mood" value="WideAwake">Wide Awake<br>
      <input type="radio" name="mood" value="Scared">Scared<br>
      <input type="radio" name="mood" value="Fearless">Fearless<br>
      <br/>
      Choose a file to upload: <input type="file" name="fileToUpload" id="fileToUpload" />
      <br/>
      <br/>
      <input type="submit" value="submit" name="submit" />
      </form>
      </form>

      </body>
      </html>



      XMLType




      <?php

      namespace AppHttpControllers;

      use IlluminateHttpRequest;

      //import the interface above

      class XMLType implements OutputType
      {
      protected $xml;
      protected $parentElement;
      protected $elementGroup;

      public function __construct()
      {
      $this->xml= new DOMDocument('1.0', 'ISO-8859-15');
      }

      public function createParent($type, $attributes = )
      {
      $this->parentElement = $this->xml->createElement($type);
      $this->addAttributes($this->parentElement, $attributes);
      return $this;
      }

      private function addAttributes(&$element, $attributes)
      {
      if (sizeof($attributes) > 0) {
      foreach($attributes as $type => $value) {
      $element->setAttribute($type, $value);
      }
      }
      }

      public function addElement($type, $element, $attributes = )
      {
      $this->elementGroup = $this->xml->createElement($type, $element);
      $this->addAttributes($this->elementGroup, $attributes);
      return $this;
      }

      public function groupParentAndElements()
      {
      $this->parentElement->appendChild($this->elementGroup);
      return $this;
      }

      public function generate()
      {
      $this->xml->appendChild($this->parentElement);
      print $this->xml->saveXML(); //here I am saving and printing but you can change to suit your needs. It is at this point it is ready to generate the XML
      }

      }









      share|improve this question















      I have set up this form with the intention of when I add details into the; title, description and emotion text fields and then press submit, this data will be stored as xml in the php file.



      I am struggling to set up the controller so that when I submit the form it actually stores this data. The example that I will provide below worked previously when I was storing it on my database, but I am now wanting to store it as xml.




      Projects Controller




      namespace AppHttpControllers;

      use IlluminateHttpRequest;

      use OrchestraParserXmlFacade as XmlParser;

      interface OutputType
      {
      public function generate();
      }

      //project class has been imported
      use AppProject;

      class ProjectsController extends Controller
      {
      public function index()
      {
      //because the class has been imported we can reference it like this rather than 'AppProject::all();'
      $projects = Project::all();



      //passing the JSON to the view

      return view ('projects.index', ['projects'=> $projects]); //can also use 'compact('projects') instead of the ['projects'=> $projects]
      }



      //new method called create

      public function create()
      {

      return view ('projects.create');

      }

      //new method called store


      public function store()
      {

      if (isset($_POST['submit'])) {
      $xmlType = new XMLType();
      $xmlType->createParent('programme', ['id' => 1])
      ->addElement('name', $_POST['name'], ['type' => 'string'])
      ->addElement('description', $_POST['description'])
      ->addElement('mood', $_POST['mood'])
      ->groupParentAndElements()
      ->createParent('others', ['id' => 2, 'attr' => 'ex'])
      ->addElement('example', 'A value here')
      ->groupParentAndElements();

      //whenever you wish to output
      $xmlType->generate();
      }



      return view('projects.upload');
      }


      //changed this from upload to show
      public function upload()
      {


      return view('projects.upload');
      }

      //changed this from upload to show
      public function show()
      {


      return view ('projects.upload', compact('user'));
      }

      public function slider()
      {


      return view ('projects.upload', compact('user'));
      }


      }



      Database.php (where I want to store submitted data as xml




      <?
      $xmlstr = <<<XML
      <?xml version="1.0" encoding="UTF-8"?>
      <programmeList>
      <programme id="1">
      <name>School of Rock</name>
      <image>images/schoolofrock.jpg</image>
      <description>After being kicked out of a rock band, Dewey Finn becomes a substitute teacher </description>
      <mood>Agitated</mood>
      </programme>
      <programme id="2">
      <name>Pitch Perfect 2</name>
      <image>images/pitchperfect2.jpg</image>
      <description>After a humiliating command performance at The Kennedy Center, the Barden Bellas enter an international competition that no American group has ever won in order to regain their status and right to perform.</description>
      <mood>Fearless</mood>
      </programme>
      </programmeList>
      XML;
      ?>



      Create.php (view)




       <link rel="stylesheet" href="CSS/app.css" type="text/css">
      <meta name = "viewport" content="width = device-width, initial-scale = 1">
      <link rel = "stylesheet" href = "https://www.w3schools.com. /lib/w3.css">
      <!DOCTYPE html>
      <html>
      <head>
      <title>Upload</title>
      </head>
      <body>
      <!-- Sky logo + Sky cinema logo -->
      <div class="box">
      <td style="text-align:center;color:black;font-size:50px;">
      <img src="/images/sky.png" title="Sky" alt="Sky" width="auto" height="125" />
      </td>
      <td style="text-align: right;position: relative; color:black;font-size:50px;">
      <img src="/images/sky_cinema.png" title="sky_cinema" alt="Sky" width="auto" height="125" />
      </td>
      </div>

      <div>
      <ul class="w3-navbar w3-black">
      <li><a href="/projects/upload">Moodslider Home</a></li>
      <li><a href="/projects/create">Upload Content</a></li>
      </ul>
      </div>


      <div>
      <h1>Upload a movie</h1>
      </div>
      <!--post request followed by an action which states where the data will be posted/returned -->

      <form method="POST" action="/projects">


      <form action="/projects/upload" method="post" enctype="multipart/form-data">
      {{ @csrf_field() }}
      <h4>Movie Title</h4><input type="text" name="name">
      <br/>
      <h4>Movie Description</h4><textarea name="description" rows="5" cols="50" wrap="physical">
      </textarea>
      <h4>Movie Emotion</h4>
      <input type="radio" name="mood" value="Agitated">Agitated<br>
      <input type="radio" name="mood" value="Calm">Calm<br>
      <input type="radio" name="mood" value="Happy">Happy<br>
      <input type="radio" name="mood" value="Sad">Sad<br>
      <input type="radio" name="mood" value="Tired">Tired<br>
      <input type="radio" name="mood" value="WideAwake">Wide Awake<br>
      <input type="radio" name="mood" value="Scared">Scared<br>
      <input type="radio" name="mood" value="Fearless">Fearless<br>
      <br/>
      Choose a file to upload: <input type="file" name="fileToUpload" id="fileToUpload" />
      <br/>
      <br/>
      <input type="submit" value="submit" name="submit" />
      </form>
      </form>

      </body>
      </html>



      XMLType




      <?php

      namespace AppHttpControllers;

      use IlluminateHttpRequest;

      //import the interface above

      class XMLType implements OutputType
      {
      protected $xml;
      protected $parentElement;
      protected $elementGroup;

      public function __construct()
      {
      $this->xml= new DOMDocument('1.0', 'ISO-8859-15');
      }

      public function createParent($type, $attributes = )
      {
      $this->parentElement = $this->xml->createElement($type);
      $this->addAttributes($this->parentElement, $attributes);
      return $this;
      }

      private function addAttributes(&$element, $attributes)
      {
      if (sizeof($attributes) > 0) {
      foreach($attributes as $type => $value) {
      $element->setAttribute($type, $value);
      }
      }
      }

      public function addElement($type, $element, $attributes = )
      {
      $this->elementGroup = $this->xml->createElement($type, $element);
      $this->addAttributes($this->elementGroup, $attributes);
      return $this;
      }

      public function groupParentAndElements()
      {
      $this->parentElement->appendChild($this->elementGroup);
      return $this;
      }

      public function generate()
      {
      $this->xml->appendChild($this->parentElement);
      print $this->xml->saveXML(); //here I am saving and printing but you can change to suit your needs. It is at this point it is ready to generate the XML
      }

      }






      php xml laravel






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 at 14:27

























      asked Nov 23 at 0:18









      Matthew

      116




      116
























          2 Answers
          2






          active

          oldest

          votes


















          0














          I would look at using SQLite to store the data as a second DB connection



          https://www.sqlite.org



          Alternatively you would need to use something like SimpleXML grab the contents of the file, add or modify the contents and then save it back to the file.



          http://php.net/manual/en/book.simplexml.php



          This would all need to go in your store method and would not allow you to use the Projects model



          Hope this helps






          share|improve this answer





























            0














            Interface



                    interface OutputType
            {
            public function generate();
            }


            XMLFile Class



                        //import the interface above

            class XMLType implements OutputType
            {
            protected $xml;

            protected $parentElement;
            protected $elementGroup;


            protected $parentList = ;
            protected $elementList = ;

            public function __construct()
            {
            $this->xml= new DOMDocument('1.0', 'ISO-8859-15');
            }

            public function createParent($type, $attributes = )
            {
            $this->parentElement = $this->xml->createElement($type);
            $this->addAttributes($this->parentElement, $attributes);

            return $this;
            }

            private function addAttributes(&$element, $attributes)
            {
            if (sizeof($attributes) > 0) {
            foreach($attributes as $type => $value) {
            $element->setAttribute($type, $value);
            }
            }
            }

            public function addElement($type, $element, $attributes = )
            {
            $this->elementGroup = $this->xml->createElement($type, $element);
            $this->addAttributes($this->elementGroup, $attributes);
            $this->elementList = $this->elementGroup;
            return $this;
            }

            /**
            * We wish to trigger this every time you want to ad d a new parent to
            * hold new elements! So the program knows to close
            * the previous one before starting a new and mesh all into one single
            * element, which we do not want to!
            **/
            public function groupParentAndElements()
            {
            foreach($this->elementList as $prevElements) {
            $this->parentElement->appendChild($prevElements);
            }

            $this->parentList = $this->parentElement;
            $this->elementList = ;

            return $this;
            }

            public function generate()
            {
            //Uses filled parentList where each parent contains its child elements to generate the XML
            foreach ($this->parentList as $prevParent) {
            $this->xml->appendChild($prevParent);
            }

            //here I am saving and printing but you can change to suit your needs.
            //It is at this point it is ready to generate the XML
            print $this->xml->saveXML();
            }

            }


            Where you wish to use the XML class



                        if (isset($_POST['submit'])) {
            $xmlType = new XMLType();
            $xmlType->createParent('programme', ['id' => 1])
            ->addElement('name', $_POST['name'], ['type' => 'string'])
            ->addElement('description', $_POST['description'])
            ->addElement('mood', $_POST['mood'])
            ->groupParentAndElements()
            ->createParent('others', ['id' => 2, 'attr' => 'ex'])
            ->addElement('example', 'A value here')
            ->groupParentAndElements();

            //whenever you wish to output
            $xmlType->generate();
            }


            Output



                        <programme id="1">
            <name type="string">
            //your post name
            </name>
            <description>
            //your post description
            </description>
            <mood>
            //your post mood
            </mood>
            </programme>
            <others id="2" attr="ex">
            <example>
            A value here
            </example>
            </others>


            To the class you can always add extra elements like notes, types etc, it will be up to your XML needs. Just need to follow the structure or even go further :)



            working code example



            Storing the XML content into a file



            interface FileCreationContract
            {
            public function create($content);
            }


            class XMLFile implements FileCreationContract
            {

            const REGEX_EXTENTION_LOOKOUT = '/.[^.]+$/';
            const EXTENTION = '.xml';

            protected $path;
            protected $filename;

            public function __construct ($filename, $path = '')
            {
            $this->filename = $filename;
            $this->parseFilename();

            if (empty($this->path)) {
            $this->path = realpath(__DIR__);
            }
            }

            /**
            * If programmer mistakes and adds extention, removes it to make sure the wrong extention was not added;
            */
            private function parseFilename()
            {
            if ( strpos($this->filename, self::EXTENTION) === FALSE ) {
            $this->filename = preg_replace(self::REGEX_EXTENTION_LOOKOUT, '', $this->filename) . self::EXTENTION;
            }

            if (empty($this->filename)) {
            $this->filename = 'default-name'.self::EXTENTION;
            }
            }

            /**
            * @return mixed
            */
            public function getPath ()
            {
            return $this->path;
            }

            /**
            * @param mixed $path
            *
            * @return XMLFile
            */
            public function setPath ($path)
            {
            $this->path = $path;
            return $this;
            }

            /**
            * @return mixed
            */
            public function getFilename ()
            {
            return $this->filename;
            }

            /**
            * @param mixed $filename
            *
            * @return XMLFile
            */
            public function setFilename ($filename)
            {
            $this->filename = $filename;
            $this->parseFilename();
            return $this;
            }

            public function create ($content)
            {
            $file = fopen($this->path.$this->filename, 'w') or die("Unable too open file!"); //I recommend throwing an exception here!
            fwrite($file, $content);
            fclose($file);
            }

            }


            At your controller, after constructing the XML Content



            ...
            $xmlFile = new XMLFile('database.php', 'path/to/your/public/folder/');
            $xmlFile->create($xmlType->generate());
            ...


            NOTE: Replace print to return on your generate() method from XMLType class



            This new class wil make sure your filename follows the expected .xml format for saving. You can always override the path to save the file at constructor level or call ->setPath('new/path/'); to replace it, before you call create();






            share|improve this answer























            • Hi, where within this would I state which file the data should be stored within?
              – Matthew
              Nov 23 at 13:33










            • Hey @Matthew! No, I would leave the XMLType class to only be responsible to originate the XML and put the responsability on where to save (if DB, curl it or save it to a file) outside of the class. You can either create a second class to handle that or add it after you use the generate() method to return the output and then save it to a file
              – Diogo Santo
              Nov 23 at 13:36










            • This because we should look for a single responsibility when handling code, specially classes so we do not want our class to be responsible to generate XML and save to a specific location
              – Diogo Santo
              Nov 23 at 13:37










            • I just noticed you wish id to be present. I will adapt the code to consider those
              – Diogo Santo
              Nov 23 at 13:42










            • Okay, thanks for the help!
              – Matthew
              Nov 23 at 13:42











            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439333%2fsubmitting-a-php-form-to-store-data-as-xml-in-a-file%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I would look at using SQLite to store the data as a second DB connection



            https://www.sqlite.org



            Alternatively you would need to use something like SimpleXML grab the contents of the file, add or modify the contents and then save it back to the file.



            http://php.net/manual/en/book.simplexml.php



            This would all need to go in your store method and would not allow you to use the Projects model



            Hope this helps






            share|improve this answer


























              0














              I would look at using SQLite to store the data as a second DB connection



              https://www.sqlite.org



              Alternatively you would need to use something like SimpleXML grab the contents of the file, add or modify the contents and then save it back to the file.



              http://php.net/manual/en/book.simplexml.php



              This would all need to go in your store method and would not allow you to use the Projects model



              Hope this helps






              share|improve this answer
























                0












                0








                0






                I would look at using SQLite to store the data as a second DB connection



                https://www.sqlite.org



                Alternatively you would need to use something like SimpleXML grab the contents of the file, add or modify the contents and then save it back to the file.



                http://php.net/manual/en/book.simplexml.php



                This would all need to go in your store method and would not allow you to use the Projects model



                Hope this helps






                share|improve this answer












                I would look at using SQLite to store the data as a second DB connection



                https://www.sqlite.org



                Alternatively you would need to use something like SimpleXML grab the contents of the file, add or modify the contents and then save it back to the file.



                http://php.net/manual/en/book.simplexml.php



                This would all need to go in your store method and would not allow you to use the Projects model



                Hope this helps







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 at 0:34









                Josh

                610111




                610111

























                    0














                    Interface



                            interface OutputType
                    {
                    public function generate();
                    }


                    XMLFile Class



                                //import the interface above

                    class XMLType implements OutputType
                    {
                    protected $xml;

                    protected $parentElement;
                    protected $elementGroup;


                    protected $parentList = ;
                    protected $elementList = ;

                    public function __construct()
                    {
                    $this->xml= new DOMDocument('1.0', 'ISO-8859-15');
                    }

                    public function createParent($type, $attributes = )
                    {
                    $this->parentElement = $this->xml->createElement($type);
                    $this->addAttributes($this->parentElement, $attributes);

                    return $this;
                    }

                    private function addAttributes(&$element, $attributes)
                    {
                    if (sizeof($attributes) > 0) {
                    foreach($attributes as $type => $value) {
                    $element->setAttribute($type, $value);
                    }
                    }
                    }

                    public function addElement($type, $element, $attributes = )
                    {
                    $this->elementGroup = $this->xml->createElement($type, $element);
                    $this->addAttributes($this->elementGroup, $attributes);
                    $this->elementList = $this->elementGroup;
                    return $this;
                    }

                    /**
                    * We wish to trigger this every time you want to ad d a new parent to
                    * hold new elements! So the program knows to close
                    * the previous one before starting a new and mesh all into one single
                    * element, which we do not want to!
                    **/
                    public function groupParentAndElements()
                    {
                    foreach($this->elementList as $prevElements) {
                    $this->parentElement->appendChild($prevElements);
                    }

                    $this->parentList = $this->parentElement;
                    $this->elementList = ;

                    return $this;
                    }

                    public function generate()
                    {
                    //Uses filled parentList where each parent contains its child elements to generate the XML
                    foreach ($this->parentList as $prevParent) {
                    $this->xml->appendChild($prevParent);
                    }

                    //here I am saving and printing but you can change to suit your needs.
                    //It is at this point it is ready to generate the XML
                    print $this->xml->saveXML();
                    }

                    }


                    Where you wish to use the XML class



                                if (isset($_POST['submit'])) {
                    $xmlType = new XMLType();
                    $xmlType->createParent('programme', ['id' => 1])
                    ->addElement('name', $_POST['name'], ['type' => 'string'])
                    ->addElement('description', $_POST['description'])
                    ->addElement('mood', $_POST['mood'])
                    ->groupParentAndElements()
                    ->createParent('others', ['id' => 2, 'attr' => 'ex'])
                    ->addElement('example', 'A value here')
                    ->groupParentAndElements();

                    //whenever you wish to output
                    $xmlType->generate();
                    }


                    Output



                                <programme id="1">
                    <name type="string">
                    //your post name
                    </name>
                    <description>
                    //your post description
                    </description>
                    <mood>
                    //your post mood
                    </mood>
                    </programme>
                    <others id="2" attr="ex">
                    <example>
                    A value here
                    </example>
                    </others>


                    To the class you can always add extra elements like notes, types etc, it will be up to your XML needs. Just need to follow the structure or even go further :)



                    working code example



                    Storing the XML content into a file



                    interface FileCreationContract
                    {
                    public function create($content);
                    }


                    class XMLFile implements FileCreationContract
                    {

                    const REGEX_EXTENTION_LOOKOUT = '/.[^.]+$/';
                    const EXTENTION = '.xml';

                    protected $path;
                    protected $filename;

                    public function __construct ($filename, $path = '')
                    {
                    $this->filename = $filename;
                    $this->parseFilename();

                    if (empty($this->path)) {
                    $this->path = realpath(__DIR__);
                    }
                    }

                    /**
                    * If programmer mistakes and adds extention, removes it to make sure the wrong extention was not added;
                    */
                    private function parseFilename()
                    {
                    if ( strpos($this->filename, self::EXTENTION) === FALSE ) {
                    $this->filename = preg_replace(self::REGEX_EXTENTION_LOOKOUT, '', $this->filename) . self::EXTENTION;
                    }

                    if (empty($this->filename)) {
                    $this->filename = 'default-name'.self::EXTENTION;
                    }
                    }

                    /**
                    * @return mixed
                    */
                    public function getPath ()
                    {
                    return $this->path;
                    }

                    /**
                    * @param mixed $path
                    *
                    * @return XMLFile
                    */
                    public function setPath ($path)
                    {
                    $this->path = $path;
                    return $this;
                    }

                    /**
                    * @return mixed
                    */
                    public function getFilename ()
                    {
                    return $this->filename;
                    }

                    /**
                    * @param mixed $filename
                    *
                    * @return XMLFile
                    */
                    public function setFilename ($filename)
                    {
                    $this->filename = $filename;
                    $this->parseFilename();
                    return $this;
                    }

                    public function create ($content)
                    {
                    $file = fopen($this->path.$this->filename, 'w') or die("Unable too open file!"); //I recommend throwing an exception here!
                    fwrite($file, $content);
                    fclose($file);
                    }

                    }


                    At your controller, after constructing the XML Content



                    ...
                    $xmlFile = new XMLFile('database.php', 'path/to/your/public/folder/');
                    $xmlFile->create($xmlType->generate());
                    ...


                    NOTE: Replace print to return on your generate() method from XMLType class



                    This new class wil make sure your filename follows the expected .xml format for saving. You can always override the path to save the file at constructor level or call ->setPath('new/path/'); to replace it, before you call create();






                    share|improve this answer























                    • Hi, where within this would I state which file the data should be stored within?
                      – Matthew
                      Nov 23 at 13:33










                    • Hey @Matthew! No, I would leave the XMLType class to only be responsible to originate the XML and put the responsability on where to save (if DB, curl it or save it to a file) outside of the class. You can either create a second class to handle that or add it after you use the generate() method to return the output and then save it to a file
                      – Diogo Santo
                      Nov 23 at 13:36










                    • This because we should look for a single responsibility when handling code, specially classes so we do not want our class to be responsible to generate XML and save to a specific location
                      – Diogo Santo
                      Nov 23 at 13:37










                    • I just noticed you wish id to be present. I will adapt the code to consider those
                      – Diogo Santo
                      Nov 23 at 13:42










                    • Okay, thanks for the help!
                      – Matthew
                      Nov 23 at 13:42
















                    0














                    Interface



                            interface OutputType
                    {
                    public function generate();
                    }


                    XMLFile Class



                                //import the interface above

                    class XMLType implements OutputType
                    {
                    protected $xml;

                    protected $parentElement;
                    protected $elementGroup;


                    protected $parentList = ;
                    protected $elementList = ;

                    public function __construct()
                    {
                    $this->xml= new DOMDocument('1.0', 'ISO-8859-15');
                    }

                    public function createParent($type, $attributes = )
                    {
                    $this->parentElement = $this->xml->createElement($type);
                    $this->addAttributes($this->parentElement, $attributes);

                    return $this;
                    }

                    private function addAttributes(&$element, $attributes)
                    {
                    if (sizeof($attributes) > 0) {
                    foreach($attributes as $type => $value) {
                    $element->setAttribute($type, $value);
                    }
                    }
                    }

                    public function addElement($type, $element, $attributes = )
                    {
                    $this->elementGroup = $this->xml->createElement($type, $element);
                    $this->addAttributes($this->elementGroup, $attributes);
                    $this->elementList = $this->elementGroup;
                    return $this;
                    }

                    /**
                    * We wish to trigger this every time you want to ad d a new parent to
                    * hold new elements! So the program knows to close
                    * the previous one before starting a new and mesh all into one single
                    * element, which we do not want to!
                    **/
                    public function groupParentAndElements()
                    {
                    foreach($this->elementList as $prevElements) {
                    $this->parentElement->appendChild($prevElements);
                    }

                    $this->parentList = $this->parentElement;
                    $this->elementList = ;

                    return $this;
                    }

                    public function generate()
                    {
                    //Uses filled parentList where each parent contains its child elements to generate the XML
                    foreach ($this->parentList as $prevParent) {
                    $this->xml->appendChild($prevParent);
                    }

                    //here I am saving and printing but you can change to suit your needs.
                    //It is at this point it is ready to generate the XML
                    print $this->xml->saveXML();
                    }

                    }


                    Where you wish to use the XML class



                                if (isset($_POST['submit'])) {
                    $xmlType = new XMLType();
                    $xmlType->createParent('programme', ['id' => 1])
                    ->addElement('name', $_POST['name'], ['type' => 'string'])
                    ->addElement('description', $_POST['description'])
                    ->addElement('mood', $_POST['mood'])
                    ->groupParentAndElements()
                    ->createParent('others', ['id' => 2, 'attr' => 'ex'])
                    ->addElement('example', 'A value here')
                    ->groupParentAndElements();

                    //whenever you wish to output
                    $xmlType->generate();
                    }


                    Output



                                <programme id="1">
                    <name type="string">
                    //your post name
                    </name>
                    <description>
                    //your post description
                    </description>
                    <mood>
                    //your post mood
                    </mood>
                    </programme>
                    <others id="2" attr="ex">
                    <example>
                    A value here
                    </example>
                    </others>


                    To the class you can always add extra elements like notes, types etc, it will be up to your XML needs. Just need to follow the structure or even go further :)



                    working code example



                    Storing the XML content into a file



                    interface FileCreationContract
                    {
                    public function create($content);
                    }


                    class XMLFile implements FileCreationContract
                    {

                    const REGEX_EXTENTION_LOOKOUT = '/.[^.]+$/';
                    const EXTENTION = '.xml';

                    protected $path;
                    protected $filename;

                    public function __construct ($filename, $path = '')
                    {
                    $this->filename = $filename;
                    $this->parseFilename();

                    if (empty($this->path)) {
                    $this->path = realpath(__DIR__);
                    }
                    }

                    /**
                    * If programmer mistakes and adds extention, removes it to make sure the wrong extention was not added;
                    */
                    private function parseFilename()
                    {
                    if ( strpos($this->filename, self::EXTENTION) === FALSE ) {
                    $this->filename = preg_replace(self::REGEX_EXTENTION_LOOKOUT, '', $this->filename) . self::EXTENTION;
                    }

                    if (empty($this->filename)) {
                    $this->filename = 'default-name'.self::EXTENTION;
                    }
                    }

                    /**
                    * @return mixed
                    */
                    public function getPath ()
                    {
                    return $this->path;
                    }

                    /**
                    * @param mixed $path
                    *
                    * @return XMLFile
                    */
                    public function setPath ($path)
                    {
                    $this->path = $path;
                    return $this;
                    }

                    /**
                    * @return mixed
                    */
                    public function getFilename ()
                    {
                    return $this->filename;
                    }

                    /**
                    * @param mixed $filename
                    *
                    * @return XMLFile
                    */
                    public function setFilename ($filename)
                    {
                    $this->filename = $filename;
                    $this->parseFilename();
                    return $this;
                    }

                    public function create ($content)
                    {
                    $file = fopen($this->path.$this->filename, 'w') or die("Unable too open file!"); //I recommend throwing an exception here!
                    fwrite($file, $content);
                    fclose($file);
                    }

                    }


                    At your controller, after constructing the XML Content



                    ...
                    $xmlFile = new XMLFile('database.php', 'path/to/your/public/folder/');
                    $xmlFile->create($xmlType->generate());
                    ...


                    NOTE: Replace print to return on your generate() method from XMLType class



                    This new class wil make sure your filename follows the expected .xml format for saving. You can always override the path to save the file at constructor level or call ->setPath('new/path/'); to replace it, before you call create();






                    share|improve this answer























                    • Hi, where within this would I state which file the data should be stored within?
                      – Matthew
                      Nov 23 at 13:33










                    • Hey @Matthew! No, I would leave the XMLType class to only be responsible to originate the XML and put the responsability on where to save (if DB, curl it or save it to a file) outside of the class. You can either create a second class to handle that or add it after you use the generate() method to return the output and then save it to a file
                      – Diogo Santo
                      Nov 23 at 13:36










                    • This because we should look for a single responsibility when handling code, specially classes so we do not want our class to be responsible to generate XML and save to a specific location
                      – Diogo Santo
                      Nov 23 at 13:37










                    • I just noticed you wish id to be present. I will adapt the code to consider those
                      – Diogo Santo
                      Nov 23 at 13:42










                    • Okay, thanks for the help!
                      – Matthew
                      Nov 23 at 13:42














                    0












                    0








                    0






                    Interface



                            interface OutputType
                    {
                    public function generate();
                    }


                    XMLFile Class



                                //import the interface above

                    class XMLType implements OutputType
                    {
                    protected $xml;

                    protected $parentElement;
                    protected $elementGroup;


                    protected $parentList = ;
                    protected $elementList = ;

                    public function __construct()
                    {
                    $this->xml= new DOMDocument('1.0', 'ISO-8859-15');
                    }

                    public function createParent($type, $attributes = )
                    {
                    $this->parentElement = $this->xml->createElement($type);
                    $this->addAttributes($this->parentElement, $attributes);

                    return $this;
                    }

                    private function addAttributes(&$element, $attributes)
                    {
                    if (sizeof($attributes) > 0) {
                    foreach($attributes as $type => $value) {
                    $element->setAttribute($type, $value);
                    }
                    }
                    }

                    public function addElement($type, $element, $attributes = )
                    {
                    $this->elementGroup = $this->xml->createElement($type, $element);
                    $this->addAttributes($this->elementGroup, $attributes);
                    $this->elementList = $this->elementGroup;
                    return $this;
                    }

                    /**
                    * We wish to trigger this every time you want to ad d a new parent to
                    * hold new elements! So the program knows to close
                    * the previous one before starting a new and mesh all into one single
                    * element, which we do not want to!
                    **/
                    public function groupParentAndElements()
                    {
                    foreach($this->elementList as $prevElements) {
                    $this->parentElement->appendChild($prevElements);
                    }

                    $this->parentList = $this->parentElement;
                    $this->elementList = ;

                    return $this;
                    }

                    public function generate()
                    {
                    //Uses filled parentList where each parent contains its child elements to generate the XML
                    foreach ($this->parentList as $prevParent) {
                    $this->xml->appendChild($prevParent);
                    }

                    //here I am saving and printing but you can change to suit your needs.
                    //It is at this point it is ready to generate the XML
                    print $this->xml->saveXML();
                    }

                    }


                    Where you wish to use the XML class



                                if (isset($_POST['submit'])) {
                    $xmlType = new XMLType();
                    $xmlType->createParent('programme', ['id' => 1])
                    ->addElement('name', $_POST['name'], ['type' => 'string'])
                    ->addElement('description', $_POST['description'])
                    ->addElement('mood', $_POST['mood'])
                    ->groupParentAndElements()
                    ->createParent('others', ['id' => 2, 'attr' => 'ex'])
                    ->addElement('example', 'A value here')
                    ->groupParentAndElements();

                    //whenever you wish to output
                    $xmlType->generate();
                    }


                    Output



                                <programme id="1">
                    <name type="string">
                    //your post name
                    </name>
                    <description>
                    //your post description
                    </description>
                    <mood>
                    //your post mood
                    </mood>
                    </programme>
                    <others id="2" attr="ex">
                    <example>
                    A value here
                    </example>
                    </others>


                    To the class you can always add extra elements like notes, types etc, it will be up to your XML needs. Just need to follow the structure or even go further :)



                    working code example



                    Storing the XML content into a file



                    interface FileCreationContract
                    {
                    public function create($content);
                    }


                    class XMLFile implements FileCreationContract
                    {

                    const REGEX_EXTENTION_LOOKOUT = '/.[^.]+$/';
                    const EXTENTION = '.xml';

                    protected $path;
                    protected $filename;

                    public function __construct ($filename, $path = '')
                    {
                    $this->filename = $filename;
                    $this->parseFilename();

                    if (empty($this->path)) {
                    $this->path = realpath(__DIR__);
                    }
                    }

                    /**
                    * If programmer mistakes and adds extention, removes it to make sure the wrong extention was not added;
                    */
                    private function parseFilename()
                    {
                    if ( strpos($this->filename, self::EXTENTION) === FALSE ) {
                    $this->filename = preg_replace(self::REGEX_EXTENTION_LOOKOUT, '', $this->filename) . self::EXTENTION;
                    }

                    if (empty($this->filename)) {
                    $this->filename = 'default-name'.self::EXTENTION;
                    }
                    }

                    /**
                    * @return mixed
                    */
                    public function getPath ()
                    {
                    return $this->path;
                    }

                    /**
                    * @param mixed $path
                    *
                    * @return XMLFile
                    */
                    public function setPath ($path)
                    {
                    $this->path = $path;
                    return $this;
                    }

                    /**
                    * @return mixed
                    */
                    public function getFilename ()
                    {
                    return $this->filename;
                    }

                    /**
                    * @param mixed $filename
                    *
                    * @return XMLFile
                    */
                    public function setFilename ($filename)
                    {
                    $this->filename = $filename;
                    $this->parseFilename();
                    return $this;
                    }

                    public function create ($content)
                    {
                    $file = fopen($this->path.$this->filename, 'w') or die("Unable too open file!"); //I recommend throwing an exception here!
                    fwrite($file, $content);
                    fclose($file);
                    }

                    }


                    At your controller, after constructing the XML Content



                    ...
                    $xmlFile = new XMLFile('database.php', 'path/to/your/public/folder/');
                    $xmlFile->create($xmlType->generate());
                    ...


                    NOTE: Replace print to return on your generate() method from XMLType class



                    This new class wil make sure your filename follows the expected .xml format for saving. You can always override the path to save the file at constructor level or call ->setPath('new/path/'); to replace it, before you call create();






                    share|improve this answer














                    Interface



                            interface OutputType
                    {
                    public function generate();
                    }


                    XMLFile Class



                                //import the interface above

                    class XMLType implements OutputType
                    {
                    protected $xml;

                    protected $parentElement;
                    protected $elementGroup;


                    protected $parentList = ;
                    protected $elementList = ;

                    public function __construct()
                    {
                    $this->xml= new DOMDocument('1.0', 'ISO-8859-15');
                    }

                    public function createParent($type, $attributes = )
                    {
                    $this->parentElement = $this->xml->createElement($type);
                    $this->addAttributes($this->parentElement, $attributes);

                    return $this;
                    }

                    private function addAttributes(&$element, $attributes)
                    {
                    if (sizeof($attributes) > 0) {
                    foreach($attributes as $type => $value) {
                    $element->setAttribute($type, $value);
                    }
                    }
                    }

                    public function addElement($type, $element, $attributes = )
                    {
                    $this->elementGroup = $this->xml->createElement($type, $element);
                    $this->addAttributes($this->elementGroup, $attributes);
                    $this->elementList = $this->elementGroup;
                    return $this;
                    }

                    /**
                    * We wish to trigger this every time you want to ad d a new parent to
                    * hold new elements! So the program knows to close
                    * the previous one before starting a new and mesh all into one single
                    * element, which we do not want to!
                    **/
                    public function groupParentAndElements()
                    {
                    foreach($this->elementList as $prevElements) {
                    $this->parentElement->appendChild($prevElements);
                    }

                    $this->parentList = $this->parentElement;
                    $this->elementList = ;

                    return $this;
                    }

                    public function generate()
                    {
                    //Uses filled parentList where each parent contains its child elements to generate the XML
                    foreach ($this->parentList as $prevParent) {
                    $this->xml->appendChild($prevParent);
                    }

                    //here I am saving and printing but you can change to suit your needs.
                    //It is at this point it is ready to generate the XML
                    print $this->xml->saveXML();
                    }

                    }


                    Where you wish to use the XML class



                                if (isset($_POST['submit'])) {
                    $xmlType = new XMLType();
                    $xmlType->createParent('programme', ['id' => 1])
                    ->addElement('name', $_POST['name'], ['type' => 'string'])
                    ->addElement('description', $_POST['description'])
                    ->addElement('mood', $_POST['mood'])
                    ->groupParentAndElements()
                    ->createParent('others', ['id' => 2, 'attr' => 'ex'])
                    ->addElement('example', 'A value here')
                    ->groupParentAndElements();

                    //whenever you wish to output
                    $xmlType->generate();
                    }


                    Output



                                <programme id="1">
                    <name type="string">
                    //your post name
                    </name>
                    <description>
                    //your post description
                    </description>
                    <mood>
                    //your post mood
                    </mood>
                    </programme>
                    <others id="2" attr="ex">
                    <example>
                    A value here
                    </example>
                    </others>


                    To the class you can always add extra elements like notes, types etc, it will be up to your XML needs. Just need to follow the structure or even go further :)



                    working code example



                    Storing the XML content into a file



                    interface FileCreationContract
                    {
                    public function create($content);
                    }


                    class XMLFile implements FileCreationContract
                    {

                    const REGEX_EXTENTION_LOOKOUT = '/.[^.]+$/';
                    const EXTENTION = '.xml';

                    protected $path;
                    protected $filename;

                    public function __construct ($filename, $path = '')
                    {
                    $this->filename = $filename;
                    $this->parseFilename();

                    if (empty($this->path)) {
                    $this->path = realpath(__DIR__);
                    }
                    }

                    /**
                    * If programmer mistakes and adds extention, removes it to make sure the wrong extention was not added;
                    */
                    private function parseFilename()
                    {
                    if ( strpos($this->filename, self::EXTENTION) === FALSE ) {
                    $this->filename = preg_replace(self::REGEX_EXTENTION_LOOKOUT, '', $this->filename) . self::EXTENTION;
                    }

                    if (empty($this->filename)) {
                    $this->filename = 'default-name'.self::EXTENTION;
                    }
                    }

                    /**
                    * @return mixed
                    */
                    public function getPath ()
                    {
                    return $this->path;
                    }

                    /**
                    * @param mixed $path
                    *
                    * @return XMLFile
                    */
                    public function setPath ($path)
                    {
                    $this->path = $path;
                    return $this;
                    }

                    /**
                    * @return mixed
                    */
                    public function getFilename ()
                    {
                    return $this->filename;
                    }

                    /**
                    * @param mixed $filename
                    *
                    * @return XMLFile
                    */
                    public function setFilename ($filename)
                    {
                    $this->filename = $filename;
                    $this->parseFilename();
                    return $this;
                    }

                    public function create ($content)
                    {
                    $file = fopen($this->path.$this->filename, 'w') or die("Unable too open file!"); //I recommend throwing an exception here!
                    fwrite($file, $content);
                    fclose($file);
                    }

                    }


                    At your controller, after constructing the XML Content



                    ...
                    $xmlFile = new XMLFile('database.php', 'path/to/your/public/folder/');
                    $xmlFile->create($xmlType->generate());
                    ...


                    NOTE: Replace print to return on your generate() method from XMLType class



                    This new class wil make sure your filename follows the expected .xml format for saving. You can always override the path to save the file at constructor level or call ->setPath('new/path/'); to replace it, before you call create();







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 23 at 16:08

























                    answered Nov 23 at 1:28









                    Diogo Santo

                    47518




                    47518












                    • Hi, where within this would I state which file the data should be stored within?
                      – Matthew
                      Nov 23 at 13:33










                    • Hey @Matthew! No, I would leave the XMLType class to only be responsible to originate the XML and put the responsability on where to save (if DB, curl it or save it to a file) outside of the class. You can either create a second class to handle that or add it after you use the generate() method to return the output and then save it to a file
                      – Diogo Santo
                      Nov 23 at 13:36










                    • This because we should look for a single responsibility when handling code, specially classes so we do not want our class to be responsible to generate XML and save to a specific location
                      – Diogo Santo
                      Nov 23 at 13:37










                    • I just noticed you wish id to be present. I will adapt the code to consider those
                      – Diogo Santo
                      Nov 23 at 13:42










                    • Okay, thanks for the help!
                      – Matthew
                      Nov 23 at 13:42


















                    • Hi, where within this would I state which file the data should be stored within?
                      – Matthew
                      Nov 23 at 13:33










                    • Hey @Matthew! No, I would leave the XMLType class to only be responsible to originate the XML and put the responsability on where to save (if DB, curl it or save it to a file) outside of the class. You can either create a second class to handle that or add it after you use the generate() method to return the output and then save it to a file
                      – Diogo Santo
                      Nov 23 at 13:36










                    • This because we should look for a single responsibility when handling code, specially classes so we do not want our class to be responsible to generate XML and save to a specific location
                      – Diogo Santo
                      Nov 23 at 13:37










                    • I just noticed you wish id to be present. I will adapt the code to consider those
                      – Diogo Santo
                      Nov 23 at 13:42










                    • Okay, thanks for the help!
                      – Matthew
                      Nov 23 at 13:42
















                    Hi, where within this would I state which file the data should be stored within?
                    – Matthew
                    Nov 23 at 13:33




                    Hi, where within this would I state which file the data should be stored within?
                    – Matthew
                    Nov 23 at 13:33












                    Hey @Matthew! No, I would leave the XMLType class to only be responsible to originate the XML and put the responsability on where to save (if DB, curl it or save it to a file) outside of the class. You can either create a second class to handle that or add it after you use the generate() method to return the output and then save it to a file
                    – Diogo Santo
                    Nov 23 at 13:36




                    Hey @Matthew! No, I would leave the XMLType class to only be responsible to originate the XML and put the responsability on where to save (if DB, curl it or save it to a file) outside of the class. You can either create a second class to handle that or add it after you use the generate() method to return the output and then save it to a file
                    – Diogo Santo
                    Nov 23 at 13:36












                    This because we should look for a single responsibility when handling code, specially classes so we do not want our class to be responsible to generate XML and save to a specific location
                    – Diogo Santo
                    Nov 23 at 13:37




                    This because we should look for a single responsibility when handling code, specially classes so we do not want our class to be responsible to generate XML and save to a specific location
                    – Diogo Santo
                    Nov 23 at 13:37












                    I just noticed you wish id to be present. I will adapt the code to consider those
                    – Diogo Santo
                    Nov 23 at 13:42




                    I just noticed you wish id to be present. I will adapt the code to consider those
                    – Diogo Santo
                    Nov 23 at 13:42












                    Okay, thanks for the help!
                    – Matthew
                    Nov 23 at 13:42




                    Okay, thanks for the help!
                    – Matthew
                    Nov 23 at 13:42


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53439333%2fsubmitting-a-php-form-to-store-data-as-xml-in-a-file%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    What visual should I use to simply compare current year value vs last year in Power BI desktop

                    Alexandru Averescu

                    Trompette piccolo