Help on SimpleXML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
thetribster
Forum Newbie
Posts: 7
Joined: Thu May 14, 2009 11:07 am

Help on SimpleXML

Post by thetribster »

I am new to this and having trouble getting to the nodes...The XML is below as well as a simple piece of code, how do I get to the candidate data?

Code: Select all

 
printf("%s", $tstr->candidate[$p]->candidate_name);
 


Here is how I load up the object.

Code: Select all

 
$response   = http_get("http://api.followthemoney.org/candidates.list.php/?key=myapideveloperkey","www.somewebsite.com");
$tstr       = simplexml_load_string($response['FILE']); 
 

Code: Select all

 
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [state] => 
            [year] => 
            [office] => 
            [district] => 
            [party] => 
            [candidate_status] => 
            [candidate_name] => 
            [sort] => 
            [page] => 0
            [next_page] => yes
            [record_count] => 200
            [origin] => Institute on Money in State Politics
            [process_timestamp] => 06-17-2009 11:30:13
            [update_timestamp] => 06-14-2009 02:37:35
        )
 
    [candidate] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [imsp_candidate_id] => 1
                            [state_postal_code] => VA
                            [state_name] => Virginia
                            [year] => 1999
                            [candidate_name] => ABBITT JR, WATKINS M
                            [candidate_status] => Won
                            [party] => DEMOCRAT
                            [office] => HOUSE
                            [district] => 059
                            [total_in_state_dollars] => 0
                            [total_out_of_state_dollars] => 0
                            [total_unknown_state_dollars] => 0
                            [party_committee_dollars] => 0
                            [candidate_leadership_committee_dollars] => 0
                            [candidate_money_dollars] => 0
                            [individual_dollars] => 0
                            [unitemized_donation_dollars] => 0
                            [public_fund_dollars] => 0
                            [non_contribution_income_dollars] => 0
                            [institution_dollars] => 0
                            [total_contribution_records] => 0
                            [total_dollars] => 0
                        )
 
                )
 
            [1] => SimpleXMLElement Object
 
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Help on SimpleXML

Post by requinix »

candidate_name is an attribute, not a child node.

Code: Select all

$tstr->candidate[$p]["candidate_name"]
thetribster
Forum Newbie
Posts: 7
Joined: Thu May 14, 2009 11:07 am

Re: Help on SimpleXML

Post by thetribster »

Great, thanks - worked perfectly. I could have sworn I tried this but obviously I didn't. Thanks.
Post Reply