如何获取“Blog-Autor”-Role和“Kommentar-Manager”-Role的角色节点?
或者有没有办法通过角色节点更动态地做到这一点?
我的 XML:
<roles>
<role role="Administrator">
<role role="Account Manager">
<role role="Blog-Autor"/>
<role role="Kommentar-Manager"/>
</role>
</role>
</roles>
还有我的 php 代码,我用它来浏览上面的 XML: (在output_roles函数中,$roles变量只是第一个 child ,但我需要两个 child )(->children()没有帮助)
public function User_Roles() {
$User_Type = $_SESSION["User_Type"];
$xml_path = "./blog_config/blog_roles.xml";
$xml_file = simplexml_load_file($xml_path);
foreach ($xml_file->role as $role) {
$role_attributes = $role->attributes();
$user_role = (string) $role_attributes->role;
if ($user_role == $User_Type) {
$this->output_roles($role);
}
}
}
private function output_roles($role) {
foreach ($role as $current_role) {
$role_ = $current_role->attributes();
$role_type = (string) $role_->role;
echo "<tr>";
echo "<td><b>" . $role_type . "</b></td>";
echo "</tr>";
$roles = $role->role;
$x = is_array($roles);
if (is_array($current_role->role)) {
$this->output_roles($current_role->role);
}
}
}
最佳答案
如果您想获取给定祖先角色的所有最内部角色,您可以使用 xpath 来完成。例如:
$User_Type = "Administrator";
$roles = $xml_file->xpath("//role[@role='$User_Type']//role[not(role)]");
以上xpath返回全部<role>
下<role role="Administrator">
没有子元素 <role>
,换句话说,最里面的<role>
元素。
关于php - 如何获取角色节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30314813/