使用XML中的特殊字符在PHP中解析XML

时间:2013-02-03 20:02:58

标签: php xml parsing

我无法解析下面的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;

有人能帮我理解我做错了吗?

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

2 个答案:

答案 0 :(得分: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);
}
?>

答案 1 :(得分:1)

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