2012-11-17 347 views
0

我有以下temp.XML文件:XML PHP解析

<?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| [email protected] Mobilna Naplata: fortumo.com</local> 
      <english>Price: 2.00 BAM + VAT 
Support: 000000000| [email protected] 
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]; 

但我什么也没得到。

回答

2

尝试:

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

你忘了几个节点:

echo '---> '.$obj -> service[0] -> countries[0] -> country[0] -> promotional_text[0] -> local[0]; 
2
echo (string) $obj->service[0]->countries[0]->country[0]->promotional_text[0]->local[0]; 

输出

以上的价格:2,00 BAM + PDV

波德rška:000000000 | [email protected] MOBILNA Naplata:fortumo.com

Codepad demo here

记住这是很好的做法呼应一个SimpleXML的值时,因为你的目标对象,以转换为字符串。

0

你可以使用xpath,像这样:

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

print_r($local[0]);