开发者

Basic PHP SimpleXML Usage Question

开发者 https://www.devze.com 2023-01-15 04:57 出处:网络
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

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 t开发者_如何学Chis string name="id" convention.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消