问题
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