user1526419
user1526419

Reputation:

Parsing XML in PHP with special chars in XML

I'm having trouble parsing the XML below to get the output that I want. I'm getting the XML below from Commission Junction and don't have control over the output.

<cj-api>
   <advertisers total-matched="609" records-returned="10" page-number="1">
      <advertiser>
         <advertiser-id>2283</advertiser-id>
         <account-status>Active</account-status>
         <seven-day-epc>0.00</seven-day-epc>
         <three-month-epc>1.11</three-month-epc>
         <language>en</language>
         <advertiser-name>
            Name here
         </advertiser-name>
         <program-url>http://website.com/</program-url>
         <relationship-status>joined</relationship-status>
         <mobile-tracking-certified>false</mobile-tracking-certified>
         <network-rank>1</network-rank>
         <primary-category>
            <parent>Financial Services</parent>
            <child>Loans</child>
         </primary-category>
         <performance-incentives>false</performance-incentives>
         <actions>
            <action>
               <name>Sale</name>
               <type>sale</type>
               <id>120</id>
               <commission>
                  <default type="item-level">USD 15.00</default>
               </commission>
            </action>
         </actions>
         <link-types>
            <link-type>Text Link</link-type>
            <link-type>Banner</link-type>
            <link-type>Content Link</link-type>
         </link-types>
      </advertiser>
   </advertisers>
</cj-api>

I'm using cURL to send the request, and receive the response. When I print the response I get the XML above, and it all looks good, but when I try to parse the information per below, I get "0" when I would expect to see "2283"

$xml = simplexml_load_string($response);
print $xml->{"cj-api"}->advertisers->advertiser->advertiser-id;

Could anyone help me understand what I'm doing wrong?

To make me extra happy, if someone could show me how to parse each element to its own variable, I would be extra greatful.

Upvotes: 1

Views: 269

Answers (2)

X Tian
X Tian

Reputation: 772

printf("id is %s", $xml->{"advertisers"}->advertiser->{"advertiser-id"});

Upvotes: 1

tftd
tftd

Reputation: 17062

This should solve your problem.

<?php

$xml_string = <<<XML
<cj-api>
   <advertisers total-matched="609" records-returned="10" page-number="1">
      <advertiser>
         <advertiser-id>2283</advertiser-id>
         <account-status>Active</account-status>
         <seven-day-epc>0.00</seven-day-epc>
         <three-month-epc>1.11</three-month-epc>
         <language>en</language>
         <advertiser-name>
            Name here
         </advertiser-name>
         <program-url>http://website.com/</program-url>
         <relationship-status>joined</relationship-status>
         <mobile-tracking-certified>false</mobile-tracking-certified>
         <network-rank>1</network-rank>
         <primary-category>
            <parent>Financial Services</parent>
            <child>Loans</child>
         </primary-category>
         <performance-incentives>false</performance-incentives>
         <actions>
            <action>
               <name>Sale</name>
               <type>sale</type>
               <id>120</id>
               <commission>
                  <default type="item-level">USD 15.00</default>
               </commission>
            </action>
         </actions>
         <link-types>
            <link-type>Text Link</link-type>
            <link-type>Banner</link-type>
            <link-type>Content Link</link-type>
         </link-types>
      </advertiser>
      <advertiser>
         <advertiser-id>2284</advertiser-id>
         <account-status>Not Active</account-status>
         <seven-day-epc>0.00</seven-day-epc>
         <three-month-epc>1.11</three-month-epc>
         <language>en</language>
         <advertiser-name>
            Name here
         </advertiser-name>
         <program-url>http://website.com/</program-url>
         <relationship-status>joined</relationship-status>
         <mobile-tracking-certified>false</mobile-tracking-certified>
         <network-rank>1</network-rank>
         <primary-category>
            <parent>Financial Services</parent>
            <child>Loans</child>
         </primary-category>
         <performance-incentives>false</performance-incentives>
         <actions>
            <action>
               <name>Sale</name>
               <type>sale</type>
               <id>120</id>
               <commission>
                  <default type="item-level">USD 15.00</default>
               </commission>
            </action>
         </actions>
         <link-types>
            <link-type>Text Link</link-type>
            <link-type>Banner</link-type>
            <link-type>Content Link</link-type>
         </link-types>
      </advertiser>
   </advertisers>
</cj-api>
XML;

$parse = simplexml_load_string($xml_string);
foreach($parse->advertisers->advertiser as $advertiser){
    $id = (string) $advertiser->{"advertiser-id"};
    var_dump($id);
}
?>

Upvotes: 1

Related Questions