0

I want to export an array to XML file in PHP:

$fxml = fopen('file.xml', 'w');
$xml = new SimpleXMLElement('<postal_codes/>');

$rows = $db->query('SELECT * FROM postal_codes ORDER BY ID');

foreach ($rows as $row) {
  if ($provinces[$row['region']] == 'test') {
    $place = $xml->addChild('place');
    foreach($fields as $key => $val) {
      $place->addChild($val, $row[$val]);
    }
  }
}

fwrite($fxml, $xml->asXML());
fclose($fxml);

The data is exported, but the values don't contain Polish characters such as: ń, ś, ł, ó etc. How to fix this? Instead of correct characters, I have: &#x15B; &#x144; &#x119;

1 Answer 1

1

That is because you load an ASCII XML document. If you provide the UTF-8 encoding it will not encode the unicode characters.

$data = "Witaj świecie";

$xml = new SimpleXMLElement("<postal_codes/>");
$xml->addChild("place", $data);

echo $xml->asXML();

$xml = new SimpleXMLElement(
    "<?xml version='1.0' encoding='utf-8'?>\n<postal_codes/>"
);
$xml->addChild("place", $data);

echo $xml->asXML();

Output:

<?xml version="1.0"?>
<postal_codes><place>Witaj &#x15B;wiecie</place></postal_codes>
<?xml version="1.0" encoding="utf-8"?>
<postal_codes><place>Witaj świecie</place></postal_codes>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.