PHP DOM:获取没有后代节点的 Nodevalue

发布于 2024-10-02 14:29:15 字数 341 浏览 1 评论 0原文

我正在使用 php dom 函数 nodeValue 来提取内容文本。但它也返回后代值:

<example>
<inhalt>123<more>45</more></inhalt>
</example>

根据我的喜好返回 inhalt 12345 和 123。我知道在 inhalt 中包含文本是没有意义的,也不是很好,但是我如何检查 inhalt 中没有像 hasNodeValue 这样的文本?

i am using the php dom function nodeValue to extract the text of content. But it returns the descendant values too:

<example>
<inhalt>123<more>45</more></inhalt>
</example>

return for inhalt 12345 and 123 as i prefer. I know that is not the sense and not very well to have text inside inhalt but how i check that there is no text in inhalt like hasNodeValue?

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

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

发布评论

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

评论(1

云巢 2024-10-09 14:29:15

正如 Phil 提到的,文本也是按节点组织的。因此,您的节点inhalt有两个子节点:一个文本节点(“123”)和一个名为more的元素节点,该元素节点也有一个文本节点(“45”)。

如果您只是想知道,元素之前是否有文本 more,请测试是否至少有一个元素,然后测试第一个元素是否为文本(应为 DOMText > 据我所知)。

如果您想知道,“根”处是否至少有一个元素,请迭代所有子元素并进行相同的测试。

http://www.php.net/manual/en/class.domtext.php

As Phil mentioned text is also organized in nodes. Therefore your node inhalt has two children: A text node ("123") and an element node named more, which also has a text node ("45").

If you just want to know, if there is text before the element more, test if there is at least one element and then test if the first element is text (should be DOMText as far as I remember).

If you like to know, if there is at least one element "at the root", iterate over all children and make the same tests.

http://www.php.net/manual/en/class.domtext.php

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