SimpleXML用于复杂XML的加载文件功能?

SimpleXML用于复杂XML的加载文件功能?

问题描述:

I am trying to use this function to get certain values back from the response.

The xml response is:

<details>

<ID>355499958</ID>
<parentID>94581</parentID>
<parentTable>acNumber</parentTable>
<title>Connected</title>
<StartTime>2013-08-20 12:30:54</StartTime>
<EndTime>2013-08-20 12:32:53</EndTime>
<connect>1.9902</connect>
<CLI>01234567890</CLI>
<dialledNumber>01234567890</dialledNumber>

<CData>
<DI N="opID" V="12345678" T="digitstring"/>
<DI N="account" V="1" T="digitstring"/>
<DI N="tID" V="1-2-3456789" T="digitstring"/>
<DI N="auth" V="test" T="digitstring"/>
<DI N="result" V="0" T="digitstring"/>
<DI N="accountID" V="" T="digitstring"/>
<DI N="tAmount" V="1000" T="digitstring"/>
<DI N="responseMessage" V="" T="digitstring"/>
</CData>

</details>

Now this is fine to pull back one of the first values etc: echo $xml->CLI;

But I need to get individual values from the CData fields.

How can I achieve this please?

You can do with simplexml_load_string or simplexml_load_file

$xml = simplexml_load_string($xmlresponse);

echo $xml->ID; // 355499958

foreach($xml->CData->DI as $value)
{
    $att = $value->attributes();
    echo $att['N']; // opID
    echo $att['V']; // 12345678
    echo $att['T']; // digitstring
}

Get specific index

tAmount is 7. line. So 6. index in array. Array indexes starts with zero.

$xml = simplexml_load_string($string);

$data = $xml->CData->DI[6]->attributes();

echo $data['N']; // tAmount
echo $data['V']; // 1000
echo $data['T']; // digitstring