Skip to content

Core\Xml\Node::text

$node->text(): tainted string

Returns the text of one XML node, as a string. For a text node, this is its characters. For a comment, it is the comment's content. For a processing instruction such as <?print fast?>, it is the part after the name, here fast.

An element returns an empty string. The text of <name>Mug</name> is in the text node inside the element, so you read it from the element's children. The document node also returns an empty string.

Entities such as &amp; are already expanded, so you get &. A CDATA section gives the text exactly as it is written inside it. The result is tainted, because it came from outside the program.

The examples below show where an element's text is, read a comment and a CDATA section, and read the values of a small settings file.

Return value

Returns tainted string. A tainted string. Entities such as &amp; and character references such as &#98; are expanded. A CDATA section gives the text written inside it.

Examples

The text is in the child

<?nvs
// Shows where the text of an element is. It is in a text node inside the element.
Core\Xml\Node $document = Core\Xml::parse('<message>Tea &amp; cake at four</message>');
foreach ($document->children() as Core\Xml\Node $message) {
// An element has no text of its own. This prints [].
echo '[', $message->text(), "]\n";
// The text node inside it has the text. `&amp;` is already expanded to `&`.
// This prints [Tea & cake at four].
foreach ($message->children() as Core\Xml\Node $child) {
echo '[', $child->text(), "]\n";
}
}
Output
[]
[Tea & cake at four]

Comments and cdata

<?nvs
// Shows the text of a comment and of a CDATA section.
// A CDATA section is a way to write `<` and `&` without entities.
string $xml = '<snippet><!-- runs in the browser --><![CDATA[if (a < b && b < c) { ok(); }]]></snippet>';
Core\Xml\Node $document = Core\Xml::parse($xml);
foreach ($document->children() as Core\Xml\Node $snippet) {
// This prints the comment's content with its spaces. Then it prints the code exactly as it is written.
foreach ($snippet->children() as Core\Xml\Node $child) {
echo $child->text(), "\n";
}
}
Output
runs in the browser
if (a < b && b < c) { ok(); }

Read a settings file

<?nvs
// Shows how to read the values of a small XML settings file into an array.
string $xml = '<settings>
<host>db.example.com</host>
<port>5432</port>
<password/>
</settings>';
array<tainted string> $settings = [];
Core\Xml\Node $document = Core\Xml::parse($xml);
foreach ($document->children() as Core\Xml\Node $root) {
foreach ($root->children() as Core\Xml\Node $setting) {
if ($setting->kind() != Core\Xml\Node::Kind::Element) {
continue;
}
// The value is the text of the text node inside the element.
// An empty element like `<password/>` has no text node, so its value stays "".
tainted string $value = '';
foreach ($setting->children() as Core\Xml\Node $child) {
$value = $value . $child->text();
}
$settings[$setting->name()] = $value;
}
}
// This prints host = db.example.com, port = 5432 and password = with nothing after it.
foreach ($settings as tainted string $name => tainted string $value) {
echo $name, ' = ', $value, "\n";
}
Output
host = db.example.com
port = 5432
password =