1

I'm reading an XML file with Simplexml and outputting a translated XML file with a different name. Google Translate outputs everything in UTF-8 and in order to properly view extended characters I need the XML Prolog to contain the encoding info:

<?xml version="1.0" encoding="UTF-8"?>

Some files have no encoding info or even no prolog at all. Is there a specific method to change or add prolog info in XML files through PHP? Thank you.

Paulo Hgo
  • 834
  • 1
  • 11
  • 26
  • have you tried this: [Getting encoding=“UTF-8” standalone=“yes” using PHP asXML and SimpleXMLElement](http://stackoverflow.com/questions/16386289/getting-encoding-utf-8-standalone-yes-using-php-asxml-and-simplexmlelement) ? – har07 Feb 13 '16 at 06:17
  • I looked at that, it seems pretty easy but it's not working for me for some reason. I get a _' Start tag expected, '<' not found'_ warning followed by a _Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML'._ I tried replacing < and > with < and gt; to no avail. I'm sure this is basic but what am I missing? – Paulo Hgo Feb 15 '16 at 05:48
  • BTW, the line of code is: `$xml = new SimpleXMLElement('');` – Paulo Hgo Feb 15 '16 at 05:48
  • As discussed implicitly in the comment there, you need to provide a root element as well : `$xml = new SimpleXMLElement('');` – har07 Feb 15 '16 at 06:51

1 Answers1

1

For example, consider the following XML string with undesired XML declaration format. You can use DOMDocument to get only the XML content without declaration, like so :

$raw = '<?xml version="1.0"?><root/>';

$xml = new DOMDocument();
$xml->loadXML($raw);
$raw_without_prolog = $xml->saveXML($xml->documentElement);

From this point, you can simply prepend your standard XML declaration to produce the desired XML in whatever type (plain string, DOMDocument, SimpleXMLElement, either one should be as easy) :

.....
.....
$prolog = '<?xml version="1.0" encoding="UTF-8"?>';

$new_xml = new DOMDocument();
$new_xml->loadXML($prolog . $raw_without_prolog);

echo $new_xml->saveXML();

eval.in demo

har07
  • 88,338
  • 12
  • 84
  • 137
  • Thank you, I understand your approach now and it seems it's going to work. I'm a beginner in PHP and am struggling with one thing in the solution above. Attributing the prolog to the variable chokes as it probably doesn't like the `` and I have to escape it somehow, don't i? I tried `htmlspecialchars` but it made the `loadXML` command fail. – Paulo Hgo Feb 15 '16 at 19:02
  • Update your question with the codes you tried.. the demo codes I posted also contains `` character and it works, so I don't know how `` causes problem in your case.. – har07 Feb 17 '16 at 08:25
  • Thanks for the sample code, it works fine for me now. – Paulo Hgo Feb 27 '16 at 06:34
  • My problem is that I was trying to load a file instead of just a string. I used your example above with load() instead of loadXML() and it worked fine. – Paulo Hgo Feb 27 '16 at 06:48