问题描述:

I am trying to read this xml content http://auroserver.com/sandbox/xml.xml as arrays using PHP

i've read some QA here but still cannot implement how to read these file completely.

I am using this code:

$url = 'http://auroserver.com/sandbox/xml.xml';

$fileContents = file_get_contents($url);

$fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);

$fileContents = trim(str_replace('"', "'", $fileContents));

$Xml = simplexml_load_string($fileContents);

$xml_array = unserialize(serialize(json_decode(json_encode((array) $Xml), 1)));

var_dump($xml_array);

using above code, it only return:

array(3) {

["Status"]=>

string(1) "1"

["Message"]=>

array(0) {

}

["Content"]=>

array(0) {

}

}

how to render all xml child values? for this things i need to read these values in the xml file:

<CardNo>6048180000002031</CardNo>

<Status>true</Status>

<Ebalance>92900</Ebalance>

<RewardPoint>6900</RewardPoint>

thank you

网友答案:

Unless you do not have any specific reason to do so, there is no need to convert the XML into an array. If your goal is just to grab the values mentioned, you could use an XPath query to get them directly from the XML:

$url            = 'http://auroserver.com/sandbox/xml.xml';
$fileContents   = trim(file_get_contents($url));

$xml = new SimpleXMLElement($fileContents);
$table = $xml->xpath('//NewDataSet/Table')[0]; # 0 = first item
echo "CardNo:      " . $table->CardNo, PHP_EOL;
echo "Status:      " . $table->Status, PHP_EOL;
echo "Ebalance:    " . $table->Ebalance, PHP_EOL;
echo "RewardPoint: " . $table->RewardPoint, PHP_EOL;

Output:

CardNo:      6048180000002031
Status:      true
Ebalance:    92900
RewardPoint: 6900
相关阅读:
Top