忽略SimpleXML中的注释节点 [英] Ignore comment node in SimpleXML

查看:58
本文介绍了忽略SimpleXML中的注释节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
使用php查找并编辑html的注释元素

我在XML文件中有一条评论.如果我使用SimpleXML加载它,然后对节点进行var_dump( var_dump($ xml-> node-> comment); ),则看起来像这样.

I have a comment in my XML file. If I load it with SimpleXML and then var_dump the node (var_dump($xml->node->comment);), it looks like this.

["comment"]=>
  object(SimpleXMLElement)#54 (0) {
}

相应的XML看起来像这样.

The corresponding XML would look like this.

<root>
  <node>
    <!-- some comment -->
  </node>
</root>

如果我想动态地对此进行解析,如何检测此SimpleXMLElement是否实际上是注释?最好不检查键是否为注释.

If I want to dynamically parse this, how can I detect if this SimpleXMLElement is in fact a comment? Preferably without checking if the key is comment.

谢谢.

更新:我不想阅读评论,我想忽略它.意识到这一点还不清楚.

Update: I don't want to read the comment, I want to ignore it. Realized this was unclear.

推荐答案

对于问题如何检测此SimpleXMLElement实际上是否是注释?" 答案是:您不能.即使您试图欺骗SimpleXML返回不支持的类型的节点,它通常也将返回其父级或文档的根.

To the question "how can I detect if this SimpleXMLElement is in fact a comment?" the answer is: you can't. Even if you attempt to trick SimpleXML into returning a node of a type it doesn't support, it will usually return its parent or the root of the document instead.

如果您绝对必须阅读注释,请使用DOM.而且,如果您对数据的格式有任何发言权,请避免将数据完全存储在注释中.

If you absolutely have to read comments, use DOM. And if you have any say over the format of your data, just avoid storing data in comments altogether.

这篇关于忽略SimpleXML中的注释节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆