0

我有以下 temp.XML 文档:

<?xml version="1.0" encoding="UTF-8"?>
<services_api_response version="2.0">
  <status>
    <code>0</code>
    <message>OK</message>
  </status>
  <service id="82dadd66a5048cc85ed0b8da1d835f2a">
    <countries>
      <country code="BA" vat="17.00" approved="true" name="Bosnia and Herzegovina">
        <prices>
          <price vat_included="false" currency="BAM" all_operators="true" amount="2.00">
            <message_profile shortcode="091810700" all_operators="true" keyword="TXT ABC">
              <operator code="BH Mobile" billing_type="MO" revenue="0.84" default_billing_status="OK" name="BH Mobile"/>
              <operator code="HT-ERONET" billing_type="MO" revenue="0.84" default_billing_status="OK" name="HT-ERONET"/>
              <operator code="M:tel" billing_type="MT" revenue="0.84" default_billing_status="Failed" name="M:tel"/>
            </message_profile>
          </price>
        </prices>
        <promotional_text>
          <local>Cena: 2,00 BAM + PDV
Podr&#353;ka: 000000000| bbb@ccc.com Mobilna Naplata: fortumo.com</local>
          <english>Price: 2.00 BAM + VAT
Support: 000000000| bbb@ccc.com
Mobile Payment by fortumo.com</english>
        </promotional_text>
      </country>
     </countries>
    </service>
</services_api_response>

和 PHP 文件:

$obj = simplexml_load_file('temp.xml');

echo '---> '.$obj -> countries[0] -> promotional_text[0] -> local[0];

但我什么也得不到。

4

4 回答 4

2

尝试:

echo '---> ' . $obj->service[0]
                   ->countries[0]
                   ->country[0]
                   ->promotional_text[0]
                   ->local[0];
于 2012-11-17T20:18:36.290 回答
2

您忘记了几个节点:

echo '---> '.$obj -> service[0] -> countries[0] -> country[0] -> promotional_text[0] -> local[0];
于 2012-11-17T20:19:34.697 回答
2
echo (string) $obj->service[0]->countries[0]->country[0]->promotional_text[0]->local[0];

输出

塞纳:2,00 BAM + PDV

波德什卡:000000000| bbb@ccc.com Mobilna Naplata: fortumo.com

键盘演示在这里

请记住,在回显 SimpleXML 值时强制转换为字符串是一种很好的做法,因为您的目标是一个对象。

于 2012-11-17T20:21:05.637 回答
0

您可以使用xpath,如下所示:

<?php
$xml = simplexml_load_file('/tmp/temp.xml');
$local = $xml->xpath('//countries/country/promotional_text/local');

print_r($local[0]);
于 2012-11-17T20:25:21.030 回答