1

我无法解析下面的 XML 以获得我想要的输出。我从 Commission Junction 获取下面的 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>
   </advertisers>
</cj-api>

我正在使用 cURL 发送请求并接收响应。当我打印响应时,我得到了上面的 XML,一切看起来都很好,但是当我尝试解析下面的信息时,当我希望看到“2283”时得到“0”

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

谁能帮我理解我做错了什么?

为了让我更加高兴,如果有人能告诉我如何将每个元素解析为它自己的变量,我会非常感激。

4

2 回答 2

1

这应该可以解决您的问题。

<?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);
}
?>
于 2013-02-03T20:41:54.783 回答
1
printf("id is %s", $xml->{"advertisers"}->advertiser->{"advertiser-id"});
于 2013-02-03T21:57:49.117 回答