I would like to reiterate that this does not work when compiled with versions of libxml lower than 20620. I found that it just presents a blank page.
<?php $string = $reader->readString(); ?>
I worked around this issue by adding the following helper function:
<?php
function read_string($reader) {
$node = $reader->expand();
return $node->textContent;
}
?>
And then using:
<?php $string = read_string($reader); ?>
XMLReader::readString
(PHP 5 >= 5.2.0)
XMLReader::readString — Reads the contents of the current node as a string
Opis
string XMLReader::readString
( void
)
Reads the contents of the current node as a string.
Parametry
Ta funkcja nie posiada parametrów.
Zwracane wartości
Returns the content of the current node as a string. Empty string on failure.
Notatki
Uwaga
Ta funkcja jest dostępna tylko wtedy, gdy PHP zostało skompilowane z biblioteką libxml 20620 lub nowszą.
Zobacz też:
- XMLReader::readOuterXML() - Retrieve XML from current node, including it self
- XMLReader::readInnerXML() - Retrieve XML from current node
- XMLReader::expand() - Returns a copy of the current node as a DOM object
Lea Hayes ¶
1 year ago
Michael Rusch ¶
4 years ago
For those who, like me, are having trouble figuring out the difference between getString() an getInnerXML(), the difference is that getString() does NOT include markup. So, for:
<foo>bar<baz>Hello, world!</baz></foo>
getString() on the <foo> node would return
bar Hello, world!
whereas getInnerXML() would return
bar<baz>Hello, world!</baz>
