I have used SimpleXMLElement for creating xml But it cannot handle &. Here is my code
$contacts = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><Contacts></Contacts>');
$name = $contacts->addChild('name', 'You & Me');
$name->addAttribute('no', '1');
echo $contacts->asXML();
And here is the output
<?xml version="1.0" encoding="UTF-8"?>
<Contacts><name no="1">You </name></Contacts>
How to solve this Question. I want a solution for all special character.
You have to replace it with e.g html code http://www.ascii.cl/htmlcodes.htm or check this http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
$contacts = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><Contacts></Contacts>');
$name = $contacts->addChild('name', 'You & Me');
$name->addAttribute('no', '1');
echo $contacts->asXML();
you can use also a function htmlspecialchars to do it
$contacts = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><Contacts></Contacts>');
$name = $contacts->addChild('name', htmlspecialchars('You & Me', ENT_QUOTES, "utf-8"));
$name->addAttribute('no', '1');
echo $contacts->asXML();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With