views:

119

answers:

3

hi

my knowledge of xml and php is somewhat basic but have been able to parse a simple xml document (one level) ~

<numbers>
   <number>1</number>
   <number>2</number>
</numbers>

i'm attempting to parse an xml document from a website providing the latest market prices for gold bullion. I think i'm going to need a paid professional but want to give it a shot

the xml file looks like this:

<envelope>
  <message type="MARKET_DEPTH_A" version="0.1">
    <market>
      <pitches>
            <pitch
            securityClassNarrative="GOLD"
            securityId="AUXLN"
            considerationCurrency="GBP"
            >
              <buyPrices>
                    <price
                    actionIndicator="B"
                    quantity="0.153"
                    limit="23477"
              />
          </buyPrices>

          <sellPrices>
                    <price
                    actionIndicator="S"
                    quantity="0.058"
                    limit="23589"
              />
          </sellPrices>

        </pitch>
      </pitches>
    </market>
  </message>
</envelope>

and simply i have no idea how to access the values within the "headings". (whatever the term is)

sounds like i'm asking for someone to do it for me, which I don't want, but I don't know what to search for ~ it doesn't look like a regular xml structure to me.

thanks!

+3  A: 

Using SimpleXML, you can get the attributes using array-notation.

For example, with your XML data, you could have some portion of code that looks like this :

$string = <<<XML
<envelope>
  <message type="MARKET_DEPTH_A" version="0.1">
    ...
    ...
  </message>
</envelope>
XML;


$xml = simplexml_load_string($string);

echo (string)$xml->message['type'] . '<br />';
echo (string)$xml->message->market->pitches->pitch['securityId'] . '<br />';
echo (string)$xml->message->market->pitches->pitch->sellPrices->price['limit'] . '<br />';

Note : I used simplexml_load_string as I had the XML data in a string variable ; but you could also use simplexml_load_file, depending on your situation/


And you'd get the following output :

MARKET_DEPTH_A
AUXLN
23589
Pascal MARTIN
+1 for giving an example.
pinaki
Looks like a great place to start. thank you very much
dave1019
You're welcome :-) ;; Have fun !
Pascal MARTIN
A: 

It really is simple XML :).. Look at the php manual for SimpleXML. Let know if you need any help other than that

pinaki
A: 

Dave

If you ever get onto larger and more complex XML documents it might be worth looking at PHP5's DomDocument

http://php.net/manual/en/class.domdocument.php

jakenoble