0

I want a basic XML generated in PHP and the file can be saved in a directory and be edited...

This is the structure:

<?xml version="1.0" encoding="UTF-8"?>
   <user>
      <id> 1254</id>      
      <userName>Melanie Woods</userName>

      <channel> 
         <host>  www.natgeo.org   </host>
         <item>  http://animals.natgeo.com/feed.xml   </item>
         <item>  http://birds.natgeo.com/feed.xml   </item>
      </channel>
     /////....The can add as many <'channels'> as they want....
     /////And as many <'items'>' in the channel as they want
       <channel> 
         <host>  www.bbc.com   </host>
         <item>  http://america.bbc.com/feed.xml   </item>
         <item>  http://asia.bbc.com/feed.xml   </item>
         <item>  http://europe.bbc.com/feed.xml   </item>
         <item>  http://australiue.bbc.com/feed.xml   </item>
      </channel>

   </user>

I have tried so many approaches since some hours now and still can't figure out the best approach to achieve this.

The challenge is how to save the file and call it when the User thinks of adding or removing an item or channel form the XML file.

Let's say they want to Add:

<channel>
<host>www.cnn.com</host>
<item>america.news.cnn.com/fedd.xml</item>
</channel>

and they wish to remove:

<item> http://asia.bbc.com/feed.xml </item> from the bbc.com <channel>

How to pull the file and add the new channel?

What am trying now is using:

<?php

$xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\" ?><user></user>");
$id= $xml->addChild('id');
$username = $xml->addChild('userName');
 for ($i = 1; $i <= 8; ++$i) {
    $channel= $xml->addChild('channel');
    $channel->addChild('host', 'www.bbc.com');
    $channel->addChild('item','http://asia.bbc.com/feed.xml'); 
} /**/

Header('Content-type: text/xml');
print($xml->asXML());
?>

This works but doesn't save the file and suffers from adding random amount of items to random number of channels instantaneous.

EDIT: One more thing, when ran, this code produces a Single line XML file difficult to read and **It does not Validate **. Validator says:

Sorry

This feed does not validate.

line 2, column 0: Undefined root element: user [help]

    <user><id/><userName/><channel><host>www.bbc.com</host><item>http://asia.bbc

...

Wish it could produce multiple lines as the sample XML above

Any suggestion?

Universal Grasp
  • 1,835
  • 3
  • 20
  • 29

1 Answers1

1

Just to save you can use $xml->asXML('path/filename.xml');

You can have a look at this post to get some help Using SimpleXML and DOMDocument Together.

Abbasi
  • 617
  • 7
  • 14
  • Sure still can't figure out how to: **call the file when the User thinks of adding or removing an item or channel or items and channels form the XML file.** .... that'll lead somewhere. Thx – Universal Grasp Apr 28 '16 at 19:39
  • Try these post to help [http://stackoverflow.com/questions/17331495/parse-and-edit-xml-file-in-php] [http://stackoverflow.com/questions/15156464/i-want-to-modify-the-existing-data-in-xml-file-using-php] – Abbasi Apr 28 '16 at 19:44
  • I think all Good... Accepting the Answer... You can UpVote the question as well. Thanks! – Universal Grasp Apr 28 '16 at 19:54