用php解析xml

时间:2011-01-18 06:28:49

标签: php xml

我有一个xml响应如下 -

<NewDataSet> <Account> <CustCode>UZ6CMAIN</CustCode> <GUID>f5b0866b-2f67-48c5-993b-c67895bb0489</GUID> <Features>0</Features> <BaseCCY>USD</BaseCCY> <LastOrderSEQ>160928459</LastOrderSEQ> <LastDealSEQ>160928461</LastDealSEQ> <OrderLotSize>100000</OrderLotSize> <MaxOrderPips>1000</MaxOrderPips> <CancelOrderPips>1</CancelOrderPips> <TradeLotSize>100000</TradeLotSize> <MaxTradeLots>25</MaxTradeLots> <TierCount>1</TierCount> <Tier1MinLots>1</Tier1MinLots> <Tier1MaxLots>50</Tier1MaxLots> <Tier1PipDifference>0</Tier1PipDifference> <Tier2MinLots>0</Tier2MinLots> <Tier2MaxLots>0</Tier2MaxLots> <Tier2PipDifference>0</Tier2PipDifference> <Tier3MinLots>0</Tier3MinLots> <Tier3MaxLots>0</Tier3MaxLots> <Tier3PipDifference>0</Tier3PipDifference> </Account> </NewDataSet>

使用以下代码 -

$xml = simplexml_load_string($result);

print_r($xml);

给出 -

SimpleXMLElement Object ( [0] => UZ6CMAIN 02267a97-4428-401d-81e1-d0010d559470 0 USD 160928459 160928461 100000 1000 1 100000 25 1 1 50 0 0 0 0 0 0 0 ) 

现在我如何访问GUID标签和BaseCCY标签等内容的各个元素。感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

这看起来很奇怪, 因为当我运行你的代码时,我得到了输出

SimpleXMLElement Object ( [Account] => SimpleXMLElement Object ( [CustCode] => UZ6CMAIN [GUID] => f5b0866b-2f67-48c5-993b-c67895bb0489 [Features] => 0 [BaseCCY] => USD [LastOrderSEQ] => 160928459 [LastDealSEQ] => 160928461 [OrderLotSize] => 100000 [MaxOrderPips] => 1000 [CancelOrderPips] => 1 [TradeLotSize] => 100000 [MaxTradeLots] => 25 [TierCount] => 1 [Tier1MinLots] => 1 [Tier1MaxLots] => 50 [Tier1PipDifference] => 0 [Tier2MinLots] => 0 [Tier2MaxLots] => 0 [Tier2PipDifference] => 0 [Tier3MinLots] => 0 [Tier3MaxLots] => 0 [Tier3PipDifference] => 0 ) )

答案 1 :(得分:0)

print ((String) $xml->Account->GUID);