user2430227
user2430227

Reputation: 303

Parse XML with PHP

This is what my XML looks like

<?xml version="1.0" encoding="UTF-8"?>
  <item>
    <date>May 2013</date>
    <html>http://www.link.to/html.html</html>
  </item>

I do this in the PHP and it kind of works

$file = '../modules/xml/nl-archive.xml';
$xml = simplexml_load_file($file);
$string = '';
foreach($xml as $item){
    $string .= '<a href="'.$item->html .'" ><li>'.$item->date .'</li></a>';
}

Just trying to get the contents of the date tag and html tag to show up for each of the item tags. Been googling and tinkering, can't get it right. What am I doing wrong?

Upvotes: 0

Views: 50

Answers (1)

putvande
putvande

Reputation: 15213

Try changing your XML to have a root with children like:

<?xml version="1.0" encoding="UTF-8"?>
<items>
    <item>
        <date>May 2013</date>
        <html>http://www.link.to/html.html</html>
    </item>
</items>

Php is just fine as you have it.

Upvotes: 3

Related Questions