2011-01-18 53 views
0

以下是從瀏覽器複製的服務器的xml響應 -使用PHP處理XML

此XML文件似乎沒有任何與其關聯的樣式信息。文檔樹如下所示。

<string><NewDataSet> 
    <Account> 
    <CustCode>UZ6CMAIN</CustCode> 
    <GUID>a01def6c-9d79-4deb-a93c-bebc8c7fbc1b</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></string> 

使用simplexml_load_string沒有幫助我。我試過

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

但沒有打印幫助。使用XPath也沒有給出任何輸出。任何幫助,所以我可以加入帳戶標籤內的個人價值?

我用

$xml = simplexml_load_string($result); 
print ((String) $xml->Account->GUID); 

沒有打印任何東西。

+0

你說 '使用simplexml_load_string沒有幫助我。'你能否告訴我們當你嘗試時發生了什麼? –

+1

在嘗試訪問子項之前,您是否忘記訪問元素? – jmort253

+0

[用php解析xml]可能的重複(http://stackoverflow.com/questions/4721120/parse-xml-with-php) –

回答

1

你可以使用DOMDocument來做到這一點,你將需要PHP5來實現這一點。

$s = '<string><NewDataSet> 
    <Account> 
    <CustCode>UZ6CMAIN</CustCode> 
    <GUID>a01def6c-9d79-4deb-a93c-bebc8c7fbc1b</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></string>'; 

// Create new DomDocumetn object 
$dom = new DOMDOcument(); 

// Load your XML as a string 
$dom->loadXML($s); 

// Create new XPath object 
$xpath = new DOMXpath($dom); 

// Query for Account elments inside NewDataSet elemts inside string elements 
$result = $xpath->query("/string/NewDataSet/Account"); 

// Note there are many ways to query XPath using this syntax 

// Iterate over the results 
foreach($result as $node) 
{ 
    // Obtains item zero, this is the first item for any elements with the name 
    // GUID and var_dump the nodeValue for that element 
    var_dump($node->getElementsByTagName("GUID")->item(0)->nodeValue); 
} 
1

不管什麼原因,SimpleXML的似乎忽視了<string></string>標籤:

<?php 

$input = '<string><NewDataSet> 
    <Account> 
    <CustCode>UZ6CMAIN</CustCode> 
    <GUID>a01def6c-9d79-4deb-a93c-bebc8c7fbc1b</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></string>'; 

$xml = simplexml_load_string($input); 
print_r($xml); 
echo (string)$xml->NewDataSet->Account->GUID; 

此打印:

SimpleXMLElement Object 
(
    [NewDataSet] => SimpleXMLElement Object 
     (
      [Account] => SimpleXMLElement Object 
       (
        [CustCode] => UZ6CMAIN 
        [GUID] => a01def6c-9d79-4deb-a93c-bebc8c7fbc1b 
        [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 
       ) 

     ) 

) 
a01def6c-9d79-4deb-a93c-bebc8c7fbc1b