我正在尝试显示我使用PHP收到的这个XML rss,它不适用于我。 任何人都可以帮忙显示这个吗?
这是XML代码:
<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>
这是我写的代码,问题是EXPR EXCH ...处于同一级别,并且在它们上运行foreach循环是一个问题:
<?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>
答案 0 :(得分:1)
这是一个很好的功能......
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>';
它回声
Address
to : james
from : jani
Heading: remainder
body: please check your mail.
我希望它有所帮助