I’ve got an XML like the following example:
<article>
<body>
<title>I <underline><bold>LOVE</bold></underline> Coding</title>
</body>
</article>
I’d like to get the full text of the node title.
$xml=simplexml_load_file("file.xml");
$title=$xml->xpath('//title')??[];
echo (string)$title;
I cannot seem to go deeper and grab the underlined/bold XML parts.
I would like the results to be a string I LOVE Coding
.
How can I achieve this? I am only getting I Coding
.
I’ve also tried xpath('string(//title)')
but got an empty result.
3
Answers
You need to strip the tags that are inside
<title>
like so:Or if your XML isn’t a file but a string:
You can call
dom_import_simplexml()
toretrieve the DOM element, then use the
textContent
property:
Result:
SimpleXML is, quite frankly, not a good interface to work with. It’s simple as in there’s not much to it, but also simple in that there’s a fair bit missing, and frequently ends up needing more work anyway.
DomDocument is more full-featured, and by far better to work with, IMO.
Output: