我有一个使用ID或类型的XML Feed。我需要选择特定节点并将其转换为变量。
XML Feed示例:
<tour>
<tourName>Amazon Riverboat Adventure</tourName>
<dossierCode>PVIIA</dossierCode>
<tripDetails>
<tripDetail type="StartFinish">ex Lima</tripDetail>
<tripDetail type="What's Included">Lots of stuff </tripDetail>
</tripDetails>
我一直在使用以下方法提取这些数据:
<?php
if(!$xml=simplexml_load_file('xmlfeed.xml')){
trigger_error('Error reading XML file',E_USER_ERROR);
}
foreach ($xml->tourName as $tourname)
foreach ($xml->dossierCode as $agentcode)
?>
但是我不确定如何将<tripDetail type="StartFinish">
提取为$ startfinish。
有人可以帮忙吗?
非常感谢!
答案 0 :(得分:0)
http://www.php.net/manual/en/simplexmlelement.attributes.php
<?php
echo $xmlAsString = <<<XML
<tour>
<tourName>Amazon Riverboat Adventure</tourName>
<dossierCode>PVIIA</dossierCode>
<tripDetails>
<tripDetail type="StartFinish">ex Lima</tripDetail>
<tripDetail type="What's Included">Lots of stuff</tripDetail>
</tripDetails>
</tour>
XML;
$xml = simplexml_load_string($xmlAsString);
foreach ($xml->tourName as $tourname) {
var_dump($tourname);
}
foreach ($xml->dossierCode as $agentcode) {
var_dump($agentcode);
}
foreach ($xml->tripDetails as $tripDetails) {
foreach ($tripDetails as $tripDetail) {
$attributes = $tripDetail->attributes();
echo 'Content of the type: ' . $attributes['type'] . PHP_EOL .
'Content of the whole tag: ' . $tripDetail . PHP_EOL;
}
}