First of all:
- The whole purpose of using a library is getting encoding and decoding done for you automatically.
- If you inject a literal
<
character, you are defining an XML tag.
Assuming you have a valid reason to care about how <
and >
are encoded, you must use CDATA:
<?php
$dom = new DOMDocument;
$dom->load("test.xml");
$titles = $dom->getElementsByTagName('title');
foreach ($titles as $title) {
$newNode = $dom->createCDATASection($title->nodeValue);
$title->nodeValue = null;
$title->appendChild($newNode);
}
$dom->save("success.xml");
... which generates:
<?xml version="1.0"?>
<section id="1">
<title id="2"><![CDATA[>]]></title>
<title id="2"><![CDATA[<]]></title>
<title id="2"><![CDATA[<]]></title>
</section>
But this document is 100% equivalent to the one you already have (except for the missing <?xml version="1.0"?>
heading).
Edit #1: entities are just a syntactic trick to encode special chars, they do not alter the node contents at all, and PHP decodes everything automatically for you:
$dom = new DOMDocument;
$dom->load("test.xml");
$titles = $dom->getElementsByTagName('title');
foreach ($titles as $title) {
var_dump($title->nodeValue);
}
... prints the decoded contents as expected:
string(1) ">"
string(1) "<"
string(1) "<"
Perhaps it's easier to see with this other example:
echo 'O\'Brian';
... prints O'Brian
and not O\'Brian
. We're encoding the string, not modifying it.
I suspect that in the whole picture there's some jQuery code like this:
$.get('ajax/data.php', function(data) {
$('.result').text(data);
});
... where there should be something like:
$.get('ajax/data.php', function(data) {
$('.result').html(data);
});
Edit #2:
I can't even image how your authoring system works (I hope you aren't using a WYSIWYG editor to actually type HTML tags) but you've asked about XML and in XML there're only two things you can do you with angle brackets:
- Data
- Tags
In DOMDocument you can create tags with createElement() and you can insert the previously created tags with e.g. appendChild().
Period.