需要帮助来解析简单的XML到PHP

时间:2022-02-20 01:49:58

i'm trying to display this XML rss i receive using PHP , and its not working for me. can anyone help displaying this?

我正在尝试显示我使用PHP收到的这个XML rss,它不适合我。有人可以帮忙显示这个吗?

this is the XML code:

这是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>

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:

这是我写的代码,问题是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>

1 个解决方案

#1


1  

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

我希望它有所帮助

#1


1  

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

我希望它有所帮助