need help to parse simple XML to PHP
i'm trying to display this XML rss i receive using PHP , and its not working for me. can anyone help displaying this?
this is the XML code:
<RESPONSE>
<EXPR>CAD</EXPR>
<EXCH>USD</EXCH>
<AMOUNT>1</AMOUNT>
<NPRICES>1</NPRICES>
<CONVERSION>
<DATE>Thu, 10 May 2001 21:00:00 GMT</DATE>
<ASK>1.5432</ASK>
<BID>1.542</BID>
</CONVERSION>
<EXPR>CAD</EXPR>
<EXCH>CAD</EXCH>
<AMOUNT>1</AMOUNT>
<NPRICES>1</NPRICES>
<CONVERSION>
<DATE>Fri, 11 May 2001 14:29:54 GMT</DATE>
<ASK>1.0000</ASK>
<BID>1.000</BID>
</CONVERSION>
</RESPONSE>
this is the code i wrote, the problem is that the EXPR EXCH... are at the same level, and running a foreach loop on them is a problem:
<?php
oandaObj = simplexml_load_file("XMLFILENNAME.xml");
$oandaArr = $oandaObj;
?>
<ul>
<?php foreach ($oandaObj as $key): ?>
<li><?php echo $oandaObj->EXPR;?></li>
<li><?php echo $oandaObj->EXCH;?></li>
<li><?php echo $oandaObj->AMOUNT;?></li>
<li><?php echo $oandaObj->NPRICES;?></li>
<li><?php echo $oandaObj->CONVERSION->DATE;?></li>
<li><?php echo $oandaObj->CONVERSION->BID;?></li>
<li><?php echo $oandaObj->CONVERSION开发者_运维技巧->ASK;?></li>
<?php endforeach;?>
</ul>
Here is a nice function to do it ...
function ReadXml($xmlstr){
$xml = simplexml_load_string($xmlstr);
echo $xml->getName()."\n";
foreach($xml->children() as $child){
echo $child->getName().': '.$child."\n";
}
}
$xmlstr= '<Address><to>James</to><from>Jani</from><heading>Reminder</heading><body>Please check your mail.</body></Address>';
it echos
Address
to : james
from : jani
Heading: remainder
body: please check your mail.
I hope it helps
精彩评论