2011-01-18 12 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 
関連する問題