简体   繁体   中英

handling xml using PHP

Here is the xml response from a server as copied from the browser -

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<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>

using simplexml_load_string did not help me. I tried

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

but nothing was printed help. Using xpath also did not give any output.Any help so i can accees individual values inside the Account tag??

i used

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

did not print anything.

You can use DOMDocument you do this, you will need PHP5 to achieve this.

$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);
}

Whatever the reason, SimpleXML seems to ignore the <string></string> tags:

<?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;

This prints:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM