我正在尝试从多个嵌套属性中提取信息(可能是错误的术语)。
XML字符串:
<?xml version='1.0' encoding='UTF-8'?>
<eveapi version="2">
<currentTime>2013-10-01 18:00:01</currentTime>
<result>
<rowset name="members" key="characterID" columns="characterID, name">
<row characterID="2234" name="Bob">
<rowset name="roles" key="roleID" columns="roleID,roleName" />
<rowset name="grantableRoles" key="roleID" columns="roleID,roleName" />
<rowset name="rolesAtHQ" key="roleID" columns="roleID,roleName" />
<rowset name="grantableRolesAtHQ" key="roleID" columns="roleID,roleName" />
<rowset name="rolesAtBase" key="roleID" columns="roleID,roleName" />
<rowset name="grantableRolesAtBase" key="roleID" columns="roleID,roleName" />
<rowset name="rolesAtOther" key="roleID" columns="roleID,roleName" />
<rowset name="grantableRolesAtOther" key="roleID" columns="roleID,roleName" />
<rowset name="titles" key="titleID" columns="titleID,titleName">
<row titleID="256" titleName="Tiger" />
</rowset>
</row>
<row characterID="555" name="John">
<rowset name="roles" key="roleID" columns="roleID,roleName" />
<rowset name="grantableRoles" key="roleID" columns="roleID,roleName" />
<rowset name="rolesAtHQ" key="roleID" columns="roleID,roleName" />
<rowset name="grantableRolesAtHQ" key="roleID" columns="roleID,roleName" />
<rowset name="rolesAtBase" key="roleID" columns="roleID,roleName" />
<rowset name="grantableRolesAtBase" key="roleID" columns="roleID,roleName" />
<rowset name="rolesAtOther" key="roleID" columns="roleID,roleName" />
<rowset name="grantableRolesAtOther" key="roleID" columns="roleID,roleName" />
<rowset name="titles" key="titleID" columns="titleID,titleName">
<row titleID="16" titleName="R & D" />
<row titleID="64" titleName="Miner" />
<row titleID="256" titleName="Tiger" />
<row titleID="16384" titleName="POS Manager" />
</rowset>
</row>
</rowset>
</result>
<cachedUntil>2013-10-01 18:57:01</cachedUntil>
</eveapi>
这是我的PHP文件:
$simpleXML = simplexml_load_string($xml); // Load XML string data into the simplexml object parser
foreach ($simpleXML->result->rowset->row as $row) {
$name = $row->attributes()->name;
$characterID = $row->attributes()->characterID;
foreach ($row->rowset as $value) {
$titleName .= $value->row->attributes()->titleName.',';
}
echo $Name.'<br>';
echo $titleName.'<br>';
}
我想为每个名字输出的内容是:
鲍勃
虎
约翰
R&amp; D,矿工,老虎,POS经理
答案 0 :(得分:1)
有时可能很难弄清楚如何访问SimpleXML的元素。
你需要这样的东西: -
foreach ($simpleXML->result->rowset->row as $row) {
$titles = [];
$name = $row['name']->__toString();
foreach($row as $attrib){
if($attrib['name']->__toString() === 'titles'){
foreach($attrib as $title){
$titles[] = $title['titleName']->__toString();
}
}
}
echo $name . ' : ' . implode(' - ', $titles) . "<br/>\n";
}
答案 1 :(得分:1)
您希望从XML树中获取查询特定数据。这最容易用Xpath完成,SimpleXMLElement也支持它:
foreach ($rows as $row) {
$titleNames = $row->xpath('*[@name="titles"]/row/@titleName');
echo $row['name'], ': ', implode(', ', $titleNames), "\n";
}
这是在那些具有 titles 作为name属性的子元素上的每个主行元素上运行xpath查询,然后获取所有行titleName属性节点 - 当在字符串上下文中使用时implode
将其属性值变为字符串。
该示例的输出:
Bob: Tiger
John: R & D, Miner, Tiger, POS Manager
代码示例(没有您的数据):
$xml = simplexml_load_string($xml);
$rows = $xml->result->rowset->row;
foreach ($rows as $row) {
$titleNames = $row->xpath('*[@name="titles"]/row/@titleName');
echo $row['name'], ': ', implode(', ', $titleNames), "\n";
}
Xpath查询对于XML文档经常出现的嵌套结构非常有效。如果你想在PHP代码中表达相同的内容,那么你将拥有更多的嵌套循环。
参见: