从特定XML节点获取信息

时间:2015-01-28 01:51:42

标签: php xml simplexml

我正在尝试读取3个特定XML节点的值(bill_codes,sent_total,clicked_unique_total)我已经做了很多测试,我觉得我需要一个有新眼睛的人来看看这个并帮我找出我的内容再也看不到..

我使用simplexml_load_string函数将XML加载到数组中。

这是我到目前为止的代码:

$xml = simplexml_load_string($content);
echo $xml->methodResponse->item->responseData->message_data->message->bill_codes;

这是我正在使用的XML(来自API调用,因此我无权修改/更新XML的结构)

<?xml version="1.0" encoding="utf-8"?>
<methodResponse>
  <item>
    <methodName>
      <![CDATA[legacy.message_stats]]>
    </methodName>
    <responseData>
      <message_data>
        <message id="2345456">
          <message_subject>
            <![CDATA[#1 Item You Should Be Hoarding in 2015]]>
          </message_subject>
          <date_sent>2014-12-18 04:01:34</date_sent>
          <message_notes>
            <![CDATA[Sample Notes]]>
          </message_notes>
          <withheld_total>0</withheld_total>
          <globally_suppressed>0</globally_suppressed>
          <suppressed_total>0</suppressed_total>
          <bill_codes>
            <![CDATA[8578]]>
          </bill_codes>
          <sent_total>734273</sent_total>
          <link_append_statement/>
          <timezone/>
          <message_name>
            <![CDATA[Sample Message Name]]>
          </message_name>
          <optout_total>4054</optout_total>
          <optout_rate_total>0.55</optout_rate_total>
          <clicked_total>5363</clicked_total>
          <clicked_unique>4350</clicked_unique>
          <clicked_rate_unique>13.71</clicked_rate_unique>
          <campaign_id>228640</campaign_id>
          <campaign_type>C</campaign_type>
          <included_groups>
            <segment id="1208891">
              <![CDATA[Segment Name Here]]>
            </segment>
          </included_groups>
          <included_smartlists></included_smartlists>
          <excluded_groups></excluded_groups>
          <excluded_smartlists></excluded_smartlists>
          <attributes></attributes>
          <link id="40278272">
            <has_name>1</has_name>
            <clicked_unique_total>4350</clicked_unique_total>
          </link>
        </message>
      </message_data>
    </responseData>
    <responseNum>
      <![CDATA[1]]>
    </responseNum>
    <responseCode>
      <![CDATA[201]]>
    </responseCode>
  </item>
</methodResponse>

1 个答案:

答案 0 :(得分:1)

不需要包含父级,只需从->item开始:

echo $xml->item->responseData->message_data->message->bill_codes;

Sample Output