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?