views:

28

answers:

2

this code creates an xml file if it does not exist:

$xmldoc = new DOMDocument();
    if(file_exists('test.xml')){
    $xmldoc->load('test.xml');
    } else {
    $xmldoc->loadXML('<root/>'); 
    }

however, i would also like encoding="UTF-8" to be appended automatically on creation of the file. how would one do this in php?

A: 

If you are trying to create a new UTF-8 encoded file, this should work:

$xmldoc = new DomDocument('1.0', 'UTF-8');
if(file_exists('test.xml')){
    $xmldoc->load('test.xml');
} else {
    $xmldoc->loadXML('<root/>'); 
}

But if you are opening a file that is not encoded in UTF-8, you'll have problems with the output... so you'll want to check the encoding of the file if it exists, and if not, you can create it as UTF-8.


OK:

<?php
    $xmlDoc = new DOMDocument('1.0', 'utf-8');
    print  $xmlDoc->saveXML();
?>

outputs:

<?xml version="1.0" encoding="utf-8"?>
NinjaCat
tried creating a new file. your code didn't work.
fuz3d
Wait... are you trying to create a UTF8 encoded file, or have the text "encoding="UTF-8" appear _in_ the file?
NinjaCat
i'm trying to create the xml file with the text `encoding="UTF-8"` appearing at the top of the file.
fuz3d
OK - the problem with my first answer was that the UTF was in caps. UTF-8 needs to appear as utf-8.
NinjaCat
thanks NinjaCat, but i don't get your code. i'm already appending the nodes, all i want to do is append the text `encoding="utf-8"` automatically to the xml file which will be created. the text `encoding="utf-8"` doesn't appear, when i tried your code.
fuz3d
I updated the code again. Are you saying if you use "$xmlDoc = new DOMDocument('1.0', 'utf-8');", that the XML header does not show utf-8 (case sensitive)? If so, I am puzzled... what version of php are you using?
NinjaCat
yes, that's what i'm saying. and if i insert arabic text, it appears all garbled in the xml file unless i explicity type `encoding="utf-8"`. using version 5.3.2 with notepad++.
fuz3d
+2  A: 

To add/change the <?xml encoding value in the output of saveHTML(), you can set the encoding property.

$xmldoc->load('test.xml');
$xmldoc->encoding= 'utf-8';
$xmldoc->save('test.xml');

However, as Artefacto said, in this case it's pointless. An XML file without an <?xml encoding declaration or a UTF-16 BOM is definitely UTF-8 and all XML parsers will read it that way(*). You will gain nothing by adding an explicit encoding="utf-8" parameter to the XML Declaration.

Whatever the method is you're using to test, it's not doing what you think it's doing. Maybe you're loading XML into a text editor and it's saving it out in a different encoding, or something? You need to look at where you're getting the strings from that are going into the DOM before you save it, and if they're not UTF-8 you need to convert them then.

(*: Well unless it's served via another protocol with a higher-priority charset specification method, like HTTP's Content-Type header. But in that case the <?xml encoding declaration is ignored anyway.)

bobince
thank you. that worked. i'm loading the xml in Notepad++. perhaps it maybe saving and outputting in a different encoding. how do i check this?
fuz3d
From Notepad++, click on the Encoding menu item.
NinjaCat
it is set by default to `Encode in UTF-8 without BOM`.
fuz3d