tags:

views:

24

answers:

1

I'm trying to write a PHP script that gets my total unread count from Google Reader. I already have it getting the XML response from Google (which also includes unread counts for each feed), but now I'm having trouble accessing the right node in order to get the number I want, just the total unread items. Here's the basic structure of the XML response from Google:

<object>
    <number name="max">1000</number>
    <list name="unreadcounts">
        <object>
            <string name="id">user/0583/state/com.google/reading-list</string>
            <number name="count">14</number>
            <number name="newestItemTimestampUsec">1283782532883071</number>
        </object>
        <object>
            <string name="id">/feed/url</string>
            <number name="count">2</number>
            <number name="newestItemTimestampUsec">1283782532881456</number>
        </object>
    </list>
</object>

How do I get that number 14 using SimpleXML? There are an unknowable number of objects in list that may proceed the reading-list object. Essentially, I want the value for count where id is user/0583/state/com.google/reading-list.

I'm trying to use the examples on PHP.net, but they don't use this string name="id" convention.

+1  A: 

The correct XPath to get to the number 14 is:

$myID = 'user/0583/state/com.google/reading-list';
$path = "//object[string[@name='id'] = '$myID']/number[@name='count']";

The XPath expression can then be fed to SimpleXML's xpath() method. From the docs:

Returns an array of SimpleXMLElement objects or FALSE in case of an error.

Tomalak
+1.. i neglected to parse the question properly and gave him the path to the wrong number haha.
prodigitalson