S
S
sawa42018-10-17 11:05:18
XML
sawa4, 2018-10-17 11:05:18

Why is SimpleXMLElement throwing char out of range?

Hello!
Can anyone come across
this code

$xmlSign = new SimpleXMLElement("<?xml version=\"1.0\"?><data></data>");
        $xml_user_info->addChild("$key",htmlspecialchars("$value"));
        return $xmlSign->asXML();

Gives an error message
SimpleXMLElement::asXML(): xmlEscapeEntities : char out of range

Given code
$xmlSign = new SimpleXMLElement("<?xml version=\"1.0\"  encoding="UTF-8" standalone="yes"?><data></data>");
        $xml_user_info->addChild("$key",htmlspecialchars("$value"));
        return $xmlSign->asXML();

Issues
org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 877; An invalid XML character (Unicode: 0x1) was found in the element content of the document.

What am I doing wrong? Where to look, the whole Google searched
If you change the code by adding:
$xmlPlain = $xmlSign->asXML();
        return htmlentities($xmlPlain, ENT_QUOTES | ENT_IGNORE, "UTF-8");

I receive
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 2; Reference is not allowed in prolog.

If:
return html_entity_decode($xmlPlain, ENT_QUOTES | ENT_IGNORE, "UTF-8");
I get
org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 877; An invalid XML character (Unicode: 0x1) was found in the element content of the document.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
krypt3r, 2018-10-17
@krypt3r

If I understood correctly

<?php
$xmlSign = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><data/>');
$xml_user_info = $xmlSign->addChild('user', 'admin');
echo $xmlSign->asXML();

At the exit:
<?xml version="1.0" encoding="UTF-8"?>
<data><user>admin</user></data>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question