我在xml文件agent.xml中有一些数据
<?xml version="1.0" encoding="ISO-8859-1"?>
<agents>
<agent>
<image> img/sara.jpg</image>
<id>1</id>
<name>Tommy Jenkin</name>
<company>CJenkins Insurance</company>
<street>Insurance150 S State Stree</street>
<city>Linkend</city>
<phone>(773) 561-4331</phone>
</agent>
<agent>
<image> img/primary-nav-logo.png</image>
<id>2</id>
<name>saka</name>
<company>LIC Insurance</company>
<street>Insurance150 S State Stree</street>
<city>Linkend</city>
<phone>(773) 561-4331</phone>
</agent>
</agents>
现在我在php中有一个名为id的变量,如下所示
$id = 1
现在我用以下代码显示这个xml数据
<?php
$xml = simplexml_load_file("agent.xml") or die("Error: Cannot create object");
foreach($xml as $agent){
function processXML($node){
foreach($node->children() as $agent => $data){
$agent= trim($agent);
if($agent=='image')
{
echo '<div><img src="'.$data.'" ></div>';
echo '<div>';
echo '</div>';
}
elseif($agent=='id')
{
echo '<div class = "Left">';
echo '<input type = "button" name="Agent" id = "'.$data.'" class = "subs-btn" value = "Select this Agent" OnClick = Selected(this.id);>';
echo '</div>';
echo '<br/>';
echo '<br/>';
}
else
{
echo '<div class = "inline1">';
echo $data;
echo '</div>';
echo '<br/>';
}
processXML($data);
}
}
processXML($xml);
}
?>
但需要来自id = 1的xml文件的数据 那我怎么能比较它并显示它 请帮忙
答案 0 :(得分:1)
使用xpath
选择带有所需<agent>
的{{1}}:
<id>