Ignore comment node in SimpleXML [duplicate]

拥有回忆 提交于 2021-02-11 06:10:25

问题


Possible Duplicate:
find and edit comment element of html with php

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) {
}

The corresponding XML would look like this.

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

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.

Thanks.

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


回答1:


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.

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.



来源:https://stackoverflow.com/questions/6424964/ignore-comment-node-in-simplexml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!