开发者

advanced xml? multi level .. php parsing

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

<numbers>
   <number>1</nu开发者_如何学Cmber>
   <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 myself.

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 O have no idea how to access the values within the "headings". (whatever the term is)

It may sound 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.


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


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜