1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

need help to parse simple XML to PHP

Discussion in 'PHP & Perl' started by bizbez, Sep 22, 2011.

  1. bizbez

    bizbez Registered Member

    Joined:
    Jan 26, 2011
    Messages:
    86
    Likes Received:
    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>
    
    
     
  2. olimix

    olimix Regular Member

    Joined:
    Jul 21, 2010
    Messages:
    232
    Likes Received:
    71
    Location:
    11h11 brothers
    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
     
  3. bizbez

    bizbez Registered Member

    Joined:
    Jan 26, 2011
    Messages:
    86
    Likes Received:
    3
    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.
     
  4. akiaki

    akiaki Registered Member

    Joined:
    Aug 8, 2011
    Messages:
    87
    Likes Received:
    10
    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.
     
  5. ``Yousef

    ``Yousef Power Member

    Joined:
    Oct 16, 2009
    Messages:
    534
    Likes Received:
    286
    Location:
    Cooking up the medicine.
    Home Page:
    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?
     
    • Thanks Thanks x 1
    Last edited: Sep 22, 2011
  6. appman360

    appman360 Newbie

    Joined:
    Sep 21, 2011
    Messages:
    12
    Likes Received:
    5
    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 Thanks x 1
  7. bizbez

    bizbez Registered Member

    Joined:
    Jan 26, 2011
    Messages:
    86
    Likes Received:
    3
    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>
     
  8. appman360

    appman360 Newbie

    Joined:
    Sep 21, 2011
    Messages:
    12
    Likes Received:
    5
    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?
     
    • Thanks Thanks x 1
  9. 67731

    67731 Regular Member

    Joined:
    Aug 27, 2011
    Messages:
    231
    Likes Received:
    47
    Occupation:
    SEO TECH - Looking to work for myself. . .
    Location:
    Las Vegas NV
  10. bizbez

    bizbez Registered Member

    Joined:
    Jan 26, 2011
    Messages:
    86
    Likes Received:
    3
    @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.
     
  11. appman360

    appman360 Newbie

    Joined:
    Sep 21, 2011
    Messages:
    12
    Likes Received:
    5
    no prob bizbez

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