My form post has a title value set as:
$title = "Company & Sons";
$xmlDoc = new DomDocument('1.0', 'utf-8');
$node = $xmlDoc->createAttribute('title');
$node->value = $title;
...
$completed = $xmlDoc ->saveXML();
When I check the saved XML it saves as:
<title> Company & Sons </title>
How can I save it as it should be &
?
If the &
character was saved as-is, you would no longer have valid XML.
Because of this, entities with special meanings are escaped in XML; hence the &
.
To get around this, you can declare your field as CDATA
:
<title>
<![CDATA[
Company & Sons
]]>
</title>
However you do not have to actually worry about the &
being escaped when deserializing or reading XML. A reader will correctly return escaped values to their original form (much the same way \u0001
unicode escapes in JSON are turned into a valid character encoding when deserializing).
tl;dr: your output is fine, don't panic.