Req #43542 [Com]: simpleXML thinks that comment is node

From: Date: Tue, 11 Mar 2014 18:06:16 +0000
Subject: Req #43542 [Com]: simpleXML thinks that comment is node
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-184729@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=43542&edit=1

 ID:                 43542
 Comment by:         nick dot strupat at gmail dot com
 Reported by:        007not at gmail dot com
 Summary:            simpleXML thinks that comment is node
 Status:             Open
 Type:               Feature/Change Request
 Package:            SimpleXML related
 Operating System:   *
 PHP Version:        5.2.5
 Block user comment: N
 Private report:     N

 New Comment:

The best solution I can think of is to put comments into the pseudo-array returned by
SimpleXMLElement::children() with the same convention as attributes (with the key
"@attributes"). Comments should be represented by the key "@comments" in the
parent's children.


Previous Comments:
------------------------------------------------------------------------
[2011-09-28 13:40:05] aapocketz at gmail dot com

I see that my $xml simplexml object I can see the comments blocks but how do I 
access and read them?  Can I modify/insert comments into the xml somehow?  

this is a print_r section:

[comment] => Array
        (
            [0] => SimpleXMLElement Object
                (
                )

            [1] => SimpleXMLElement Object
                (
                )

            [2] => SimpleXMLElement Object
                (
                )

            [3] => SimpleXMLElement Object
                (
                )

            [4] => SimpleXMLElement Object
                (
                )

            [5] => SimpleXMLElement Object
                (
                )

            [6] => SimpleXMLElement Object
                (
                )

            [7] => SimpleXMLElement Object
                (
                )

------------------------------------------------------------------------
[2009-06-13 06:57:45] jbeauwalker at gmail dot com

It seems that the individual who reported this does not like this behaviour but I actually want
access to comments. [I'm managing Google Earth kml files and...well the detailed explanation is
lengthy]. 

Unfortunately the current behaviour doesn't seem to satisfy either of us because while comments
appear as nodes I can't seem to get access to their content. At the risk of disturbing the
peace of the purists, might I suggest something along the lines that if a comment is placed and
formatted like a node on a separate line -- eg:

    <node>
        <!-- comment -->
        <a />
    </node>
That it appear as a node (->node->comment) with a value with accessible content, but if placed
on another line it is simply ignored. 

        <node>   <!-- this is ignored -->

I can hear the complaints already but what I'm really trying to suggest is some solution with a
bit of imagination where a comment can be identified as such and its value obtained.

   <node <!--

------------------------------------------------------------------------
[2008-01-11 17:51:49] 007NOT at gmail dot com

helly wrote:
>The comment is a node.
SimpleXMLElement->children() do not think so !!! where is the TRUE ?

exsample:
<?php
$string = <<<XML
<?xml version='1.0'?>
<document>
 <node>
  <!-- comment -->
  <a />
 </node>
</document>
XML;
$xml = simplexml_load_string($string);

var_dump($xml->node);
$i = 0;
foreach ($xml->node as $node)
{
	$i++;
}
echo $i . "\n";

var_dump((array) $xml->node);
$i = 0;
foreach ((array) $xml->node as $node)
{
	$i++;
}
echo $i . "\n";

var_dump($xml->node->children());
$i = 0;
foreach ((array) $xml->node->children() as $node)
{
	$i++;
}
echo $i . "\n";

------------------------------------------------------------------------
[2007-12-23 20:07:58] 007NOT at gmail dot com

>The comment is a node. 
comment isn't node, it's nothing
exsampe:
<node></node> - node with empty value
<node><!-- comment --></node> - node with empty value
<node><!-- comment --> </node> - but not empty node thats has value
"space"

in :
>$i = 0;
>foreach ((array) $xml->node as $node)
>{
>	$i++;
>}
>echo $i . "\n";
he we checks child nodes, but not checks that node exists

see one more test:
<?php
$string = <<<XML
<?xml version='1.0'?>
<document>
 <node><!-- comment --></node>
 <otherNode></otherNode>
 <comment>value</comment>
</document>
XML;
$xml = simplexml_load_string($string);

$i = 0;
foreach ((array) $xml->node as $node)
{
	$i++;
}
echo $i . "\n";

$dom= new DOMDocument();
$dom->loadXML($string);
$Xpath = new domxpath($dom);
$NodeList = $Xpath->query('/document');
foreach ($NodeList as $Node)
{
	foreach ($Node->childNodes as $cNode)
	{
	echo 'Name: ' . $cNode->nodeName . ' Value: ' . $cNode->nodeValue .
PHP_EOL;
	if ($cNode->childNodes->length > 1) echo 'Has childNodes' . PHP_EOL;
	}
}

>What we actually need is a way to figure out the xml type of a SimpleXMLElement instance
>(Element, Comment,...). 
may be you make new metod

------------------------------------------------------------------------
[2007-12-20 14:39:45] helly@php.net

The comment is a node. What we actually need is a way to figure out the xml type of a
SimpleXMLElement instance (Element, Comment,...). This will also have to return the internal SXE
state (iterator for something or direct value).

------------------------------------------------------------------------


The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at

    https://bugs.php.net/bug.php?id=43542


-- 
Edit this bug report at https://bugs.php.net/bug.php?id=43542&edit=1


Thread (13 messages)

« previous php.bugs (#184729) next »