使用 PHP 的 SimpleXML 访问元素的父元素?

发布于 2024-08-19 23:44:28 字数 256 浏览 4 评论 0原文

我正在迭代一组 SimpleXML 对象,但不知道如何访问每个对象的父节点。这就是我想要的:

$divs = simplexml->xpath("//div");
foreach ($divs as $div)
{
   $parent_div = $div->get_parent_node(); // Sadly, there's no such function.
}

似乎必须有一个相当简单的方法来做到这一点。

I'm iterating through a set of SimpleXML objects, and I can't figure out how to access each object's parent node. Here's what I want:

$divs = simplexml->xpath("//div");
foreach ($divs as $div)
{
   $parent_div = $div->get_parent_node(); // Sadly, there's no such function.
}

Seems like there must be a fairly easy way to do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘿看小鸭子会跑 2024-08-26 23:44:28

您可以运行一个简单的 XPath 查询来获取它:

$parent_div = $div->xpath("parent::*");

由于这是 Simplexml,它只有元素和属性节点,并且父节点只能是元素而不能是属性,因此可以使用缩写语法:(

$parent_div = $div->xpath("..");

通过: 常见 Xpath 秘籍 - SimpleXML 类型速查表(2013 年 2 月;作者 hakre)< /em> )

You could run a simple XPath query to get it:

$parent_div = $div->xpath("parent::*");

And as this is Simplexml and it only has element and attribute nodes and a parent node can only be an element and never an attribute, the abbreviated syntax can be used:

$parent_div = $div->xpath("..");

(via: Common Xpath Cheats - SimpleXML Type Cheatsheet (Feb 2013; by hakre) )

烦人精 2024-08-26 23:44:28

$div->get_parent_node(); // 遗憾的是,没有这样的函数。

请注意,您可以扩展 SimpleXML 来实现这一点。例如:

class my_xml extends SimpleXMLElement
{
    public function get_parent_node()
    {
        return current($this->xpath('parent::*'));
    }
}

现在您所要做的就是首先修改用于创建 SimpleXMLElement 的代码:

$foo = new SimpleXMLElement('<foo/>');
// becomes
$foo = new my_xml('<foo/>');

$foo = simplexml_load_string('<foo/>');
// becomes
$foo = simplexml_load_string('<foo/>', 'my_xml');

$foo = simplexml_load_file('foo.xml');
// becomes
$foo = simplexml_load_file('foo.xml', 'my_xml');

最好的部分是 SimpleXML 将自动且透明地返回此文档的 my_xml 对象,因此您不必更改任何其他内容,这使得您的 get_parent_node() 方法可链接:

// returns $grandchild's parent's parent
$grandchild->get_parent_node()->get_parent_node();

$div->get_parent_node(); // Sadly, there's no such function.

Note that you can extend SimpleXML to make it so. For example:

class my_xml extends SimpleXMLElement
{
    public function get_parent_node()
    {
        return current($this->xpath('parent::*'));
    }
}

And now all you have to do is modify the code you use to create your SimpleXMLElement in the first place:

$foo = new SimpleXMLElement('<foo/>');
// becomes
$foo = new my_xml('<foo/>');

$foo = simplexml_load_string('<foo/>');
// becomes
$foo = simplexml_load_string('<foo/>', 'my_xml');

$foo = simplexml_load_file('foo.xml');
// becomes
$foo = simplexml_load_file('foo.xml', 'my_xml');

The best part is that SimpleXML will automatically and transparently return my_xml objects for this document, so you don't have to change anything else, which makes your get_parent_node() method chainable:

// returns $grandchild's parent's parent
$grandchild->get_parent_node()->get_parent_node();
魂归处 2024-08-26 23:44:28

如果没记错的话,xpath() 调用将返回一个或多个 SimpleXMLElements。如果是这种情况,那么您也许可以使用以下内容:

$div->xpath( '..' );
# or
$div->xpath( 'parent::*' );

If memory serves, an xpath() call returns one or more SimpleXMLElements. If that's the case, then you may be able to use something like:

$div->xpath( '..' );
# or
$div->xpath( 'parent::*' );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文