need help to parse simple XML to PHP

bizbez

Registered Member
Joined
Jan 26, 2011
Messages
86
Reaction score
3
hi,

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:
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 sample level so running a foreach loop is a problem.
Code:
<?php
oandaObj = simplexml_load_file("XMLFILENNAME.xml");
$oandaArr = $oandaObj;
?>
<ul>
<?php foreach ($oandaArr as $key): ?>
<li><?php echo $oandaArr->EXPR;?></li>
<li><?php echo $oandaArr->EXCH;?></li>
<li><?php echo $oandaArr->AMOUNT;?></li>
<li><?php echo $oandaArr->NPRICES;?></li>
<li><?php echo $oandaArr->CONVERSION->DATE;?></li>
<li><?php echo $oandaArr->CONVERSION->BID;?></li>
<li><?php echo $oandaArr->CONVERSION->ASK;?></li>



<?php endforeach;?>
</ul>
 
PHP:
<?php oandaObj = simplexml_load_file("XMLFILENNAME.xml"); $oandaArr = $oandaObj; ?> <ul>
<?php
foreach ($oandaArr as $key) {
?>
<li><?php echo $key->EXPR; ?></li> 
<li><?php echo $key->EXCH;?></li> 
<li><?php echo $key->AMOUNT;?></li> 
<li><?php echo $key->NPRICES;?></li> 
<li><?php echo $key->CONVERSION->DATE;?></li> 
<li><?php echo $key->CONVERSION->BID;?></li> 
<li><?php echo $key->CONVERSION->ASK;?></li>    
<?php

 }

?> </ul>
i think
 
hi thanks,
yes this works for the first cycle of the params.
but if you can see, after the </CONVERSION> tag, the same params are being called under the same hierarchy, so if you run FOREACH loop. it duplicates the output.
 
Ask the sender to send valid XML.

Or you can parse the XML yourself, in which case you can make some reasonable guesses about it if the format is generally the same.
 
You should probably drop this question on StackOverflow. Chances are, you'll get a good answer and have this wrapped up in less than 10 minutes. Also, have you tried dumping $oandaArr to see the structure?
 
Last edited:
Something like this maybe ?

PHP:
<?php

$string = "
<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>
";
$oandaObj = new SimpleXMLElement($string);
foreach ($oandaObj->CONVERSION as $key => $value) {
?>
<ul>
<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 $value->DATE;?></li> 
<li><?php echo $value->BID;?></li> 
<li><?php echo $value->ASK;?></li>
</ul>  
<?php } ?>
 
thanks given.

it doesn't work properly: for example - it doesn't print these ones: (CAD CAD)
<EXPR>CAD</EXPR>
<EXCH>CAD</EXCH>
<AMOUNT>1</AMOUNT>
<NPRICES>1</NPRICES>


the loop runs only on: (CAD USD)

<EXPR>CAD</EXPR>
<EXCH>USD</EXCH>
<AMOUNT>1</AMOUNT>
<NPRICES>1</NPRICES>
 
This should print everything

PHP:
<?php
$string = "
<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>
";
$oandaObj = new SimpleXMLElement($string);
?>
<ul>
<?php foreach ($oandaObj as $key => $value) { ?>
    <?php if ($key != 'CONVERSION') { ?>
    <li><?php echo $value; ?></li>
    <?php } else { ?>
        <?php foreach ($value as $conv) { ?>
            <li><?php echo $conv;?></li>
        <?php } ?>
    <?php } ?>

<?php } ?>
</ul>
like this
Code:
<ul> <li>CAD</li>  <li>USD</li>  <li>1</li>  <li>1</li>  <li>Thu, 10 May 2001 21:00:00 GMT</li> <li>1.5432</li> <li>1.542</li>  <li>CAD</li>  <li>CAD</li>  <li>1</li>  <li>1</li>  <li>Fri, 11 May 2001 14:29:54 GMT</li> <li>1.0000</li> <li>1.000</li>  </ul>
that what you want?
 
@appman360 works beautifully!

just got to ask - I had to run two foreach loops since this is a two dimensional array? that's what i missed?

thanks given. thanks bro.
 
no prob bizbez

yep, it contains a nested array - RESPONSE being the first tag with CONVERSION inside it
 
Back
Top