tags:

views:

39

answers:

3

this page shown an xml file and im trying to use simplexml to parse the data out and print it. what am i missing? cause all it does is show a blank page when i run it.

<?php
$url = "http://api.scribd.com/api?method=docs.getList&amp;api_key=somestring";

$xml = new SimpleXMLElement($url,NULL,true);

foreach($xml -> result as $value) {


    echo $value->doc_id."<br/>";
    echo $value->access_key."<br/>";
    echo $value->secret_password."<br/>";
    echo $value->title."<br/>";



}

?>
A: 

(Not quite an answer, but...) Add some boilerplate text before, after and inside the foreach loop to see which bits are executing, and how often the loop runs.

Marcelo Cantos
whats a boilerplate?
michael
@ida, some fixed content that is guaranteed to be visible on the page.
Marcelo Cantos
A: 

<result> is not a child of <rsp> but of <resultset>

foreach($xml->resultset->result as $value) {
VolkerK
A: 

You should always name your PHP variables after the node they represent. In your case, the root node is <rsp/> therefore the variable that holds the root node should be $rsp. This way, you can easily recognize that there's an error in the way you're trying to access the <result/> nodes:

$rsp = simplexml_load_file($url);

foreach ($rsp->resultset->result as $result)
{
    echo $result->doc_id, "<br/>\n";
}

There, no ambiguity anymore.

<rsp>
    <resultset>
        <result />
    </resultset>
</rsp>

PHP:
$rsp->resultset->result

XPath:
/rsp/resultset/result

And no wondering later on what $value refers to.

Josh Davis