Matt
Matt

Reputation: 4190

beginner attempting to read xml into php

I have an xml feed located here that I am trying to read into a php script, then cycle through the <packages>, and sum the <downloads>. I've attempted to do this using DOMDocument, but have thus far failed.

the basic method i've been trying to use is as follows

<?php
$dom = new DomDocument;
$dom->loadXML('http://www.phogue.net/feed');
$packages = $dom->getElementsByTagName('package');
foreach($packages as $item)
{
    echo $item->getAttribute('uid').'<br>';
}
?>

The above code is meant to just print out the name of each item, but its not working. I am currently getting the following error

Warning: DOMDocument::loadXML() [domdocument.loadxml]: Start tag expected, '<' not found in Entity, line: 1 in /home/a8744502/public_html/userbar.php on line 3

WORKING CODE:

<?php
$dom = new DomDocument;
$dom->load('http://www.phogue.net/feed/');
$package = $dom->getElementsByTagName('package');
$value=0;

foreach ($package as $plugin) {

    $downloads = $plugin->getElementsByTagName("downloads");
    $download = $downloads->item(0)->nodeValue;

    $authors = $plugin->getElementsByTagName("author");
    $author = $authors->item(0)->nodeValue;
    if($author == "Zaeed")
    {
        $value += $download;
    }
}
echo $value;
?>

Upvotes: 2

Views: 5382

Answers (2)

Phil
Phil

Reputation: 164901

DOMDocument::loadXML() expects a string of XML. Try DOMDocument::load() instead - http://www.php.net/manual/en/domdocument.load.php

Keep in mind that to open an XML file via HTTP, you will need the appropriate wrapper enabled.

Upvotes: 8

uadnal
uadnal

Reputation: 11445

You have a open parenthesis at the beginning of your echo.

Upvotes: 1

Related Questions