1

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.

Community
  • 1
  • 1
Markus Hedlund
  • 23,374
  • 22
  • 80
  • 109
  • @Gordon: Not really a duplicate? But one could possibly use that method to solve this – Markus Hedlund Jun 21 '11 at 12:29
  • SimpleXml can use Xpath via the xpath() method so detecting comments should work. I dont think SimpleXml can create Comment nodes though (at least that's what I answered in [Is it possible to insert a Comment node in SimpleXml](http://stackoverflow.com/questions/2139012/is-it-possible-to-insert-a-comment-tag-into-an-xml-using-simplexml/2139049#2139049)) – Gordon Jun 21 '11 at 12:36
  • @Gordon: Yes as I said, the method you presented in that answer probably can be applied here as well :) Creating a comment is of no interest to me right now, but that's good to know. – Markus Hedlund Jun 21 '11 at 12:58
  • Im not sure this can be used to detect a comment. IIRC DOM cannot import comment nodes from SimpleXml. And //comments() will result in an empty array. You could do //comments()/.. to test for the parent but Im not sure this helps much? – Gordon Jun 21 '11 at 13:41

1 Answers1

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.

Josh Davis
  • 28,400
  • 5
  • 52
  • 67
  • 1
    I don't want to read the comment, I want my `xmlToArray` function to ignore it, and I don't know how when it looks like any other node (SimpleXMLElement) – Markus Hedlund Jun 21 '11 at 14:13
  • What kind of code returns you comments? It requires some very specific code to get comments from SimpleXML. To the best of my knowledge, only `xpath('//node()')` would do that, and in that case all you have to do is filter them out with a predicate such as `xpath('//node()[not(comment())]')`. In short: SimpleXML mostly ignores comments so you shouldn't have to worry about them. – Josh Davis Jun 21 '11 at 16:02
  • The XML is user provided. If SimpleXML ignored comments, I wouldn't have this issue. Will try to use `comment()`. – Markus Hedlund Jun 28 '11 at 06:12
  • Since the question is marked as a duplicate, I can't answer but you can find the solution at https://stackoverflow.com/a/51327150/2732184 – bmatovu Jul 13 '18 at 16:38