Discussion:
Issues with simplexml_load_string()
Christoph Boget
2011-10-06 20:41:35 UTC
Permalink
It seems like when dealing with nodes which have CDATA, I cannot get
access to both the attributes of that node and the CDATA of that node
with the same simplexml_load_string() call. Consider the following:

$previewString =
'<root><message><![CDATA[lkjlkjklj]]></message><buttons><button
name="Add Me"><![CDATA[This is my free form
text]]></button></buttons></root>';

When passing LIBXML_NOCDATA:

echo '<pre>' . print_r( json_decode( json_encode(
(array)simplexml_load_string( $previewString, 'SimpleXMLElement',
LIBXML_NOCDATA | LIBXML_NOBLANKS )), 1 ), TRUE ) . '</pre>';

yields the following output :

Array
(
[message] => lkjlkjklj
[buttons] => Array
(
[button] => This is my free form text
)
)

So I get the CDATA but I don't get the "name" attribute for the button.

When not using LIBXML_NOCDATA:

echo '<pre>' . print_r( json_decode( json_encode(
(array)simplexml_load_string( $previewString, 'SimpleXMLElement',
LIBXML_NOBLANKS )), 1 ), TRUE ) . '</pre>';

yields the following output :

Array
(
[message] => Array
(
)
[buttons] => Array
(
[button] => Array
(
[@attributes] => Array
(
[name] => Add Me
)
)
)
)

I get the attributes but not the CDATA. Looking at the doc page for
simplexml_load_string()
(http://us.php.net/manual/en/function.simplexml-load-string.php and
http://us.php.net/manual/en/libxml.constants.php), I don't see a way
to get access to both. Am I missing something? Or is this really not
possible?

thnx,
Christoph
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph Boget
2011-10-06 20:50:16 UTC
Permalink
It seems like when dealing with nodes which have CDATA, I cannot get
access to both the attributes of that node and the CDATA of that node
with the same simplexml_load_string() call. Consider the following:

$previewString =
'<root><message><![CDATA[lkjlkjklj]]></message><buttons><button
name="Add Me"><![CDATA[This is my free form
text]]></button></buttons></root>';

When passing LIBXML_NOCDATA:

echo '<pre>' . print_r( json_decode( json_encode(
(array)simplexml_load_string( $previewString, 'SimpleXMLElement',
LIBXML_NOCDATA | LIBXML_NOBLANKS )), 1 ), TRUE ) . '</pre>';

yields the following output :

Array
(
[message] => lkjlkjklj
[buttons] => Array
(
[button] => This is my free form text
)
)

So I get the CDATA but I don't get the "name" attribute for the button.

When not using LIBXML_NOCDATA:

echo '<pre>' . print_r( json_decode(
json_encode((array)simplexml_load_string( $previewString,
'SimpleXMLElement', LIBXML_NOBLANKS )), 1 ), TRUE ) . '</pre>';

yields the following output :

Array
(
[message] => Array
(
)
[buttons] => Array
(
[button] => Array
(
[@attributes] => Array
(
[name] => Add Me
)
)
)
)

I get the attributes but not the CDATA. Looking at the doc page for
simplexml_load_string()
(http://us.php.net/manual/en/function.simplexml-load-string.php and
http://us.php.net/manual/en/libxml.constants.php), I don't see a way
to get access to both. Am I missing something? Or is this really not
possible?

thnx,
Christoph
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Benjamin Hawkes-Lewis
2011-10-07 06:53:44 UTC
Permalink
Post by Christoph Boget
It seems like when dealing with nodes which have CDATA, I cannot get
access to both the attributes of that node and the CDATA of that node
$previewString =
'<root><message><![CDATA[lkjlkjklj]]></message><buttons><button
name="Add Me"><![CDATA[This is my free form
text]]></button></buttons></root>';
echo '<pre>' . print_r( json_decode( json_encode(
(array)simplexml_load_string( $previewString, 'SimpleXMLElement',
LIBXML_NOCDATA | LIBXML_NOBLANKS )), 1 ), TRUE ) . '</pre>';
Array
(
  [message] => lkjlkjklj
  [buttons] => Array
      (
          [button] => This is my free form text
      )
)
So I get the CDATA but I don't get the "name" attribute for the button.
http://us.php.net/manual/en/function.simplexml-load-string.php#80855 maybe?

--
Benjamin Hawkes-Lewis
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph Boget
2011-10-07 12:47:55 UTC
Permalink
Post by Benjamin Hawkes-Lewis
http://us.php.net/manual/en/function.simplexml-load-string.php#80855 maybe?
Thanks for that. I guess I should have scrolled a little further
down. It's so crazy that it works that way. Unless you export the
actual element (and not it's ancestors), you don't see the data at
all.

thnx,
Christoph
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...