need help to parse simple XML to PHP

Discussion in 'PHP' started by datrader, Sep 22, 2011.

  1. #1
    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:
    
    <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>
    
    Code (markup):


    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.
    
    
    <?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>
    
    
    Code (markup):
     
    datrader, Sep 22, 2011 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    This isn't a valid xml format, or at least not one that is logically formed.

    There's nothing to associate the
    <EXPR />
    <EXCH />
    <AMOUNT />
    <NPRICES />
    with anything.

    Where did this feed come from.
     
    jestep, Sep 22, 2011 IP
  3. jevchance

    jevchance Peon

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    2
    Trophy Points:
    0
    #3
    jevchance, Sep 22, 2011 IP
  4. SheetalCreation

    SheetalCreation Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    0
    #4
    Given xml is not logically formed...

    There's nothing to associate the
    <EXPR />
    <EXCH />
    <AMOUNT />
    <NPRICES />
    with anything.
    it should be in following format :

    <RESPONSE>
    <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>
     
    SheetalCreation, Sep 27, 2011 IP
  5. SheetalCreation

    SheetalCreation Peon

    Messages:
    42
    Likes Received:
    1
    Best Answers:
    3
    Trophy Points:
    0
    #5
    Given xml is not logically formed...

    There's nothing to associate the
    <EXPR />
    <EXCH />
    <AMOUNT />
    <NPRICES />
    <CONVERSION>
    with anything.
    it should be in following format :

    <RESPONSES>
    <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>
    </RESPONSE>
    <RESONSE>
    <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>
    </RESPONSES>


    Sheetal
     
    SheetalCreation, Sep 27, 2011 IP
  6. datrader

    datrader Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    @Sheetal - thanks, and you're right, its not a proper XML format, that's my problem with running a loop around it, some params don't respond well.
    I think this XML , the params should be accessed manually...
     
    datrader, Sep 28, 2011 IP