MMK
MMK

Reputation: 365

Saving XML file with UTF-8 encoding

I want to store the data into XML file with UTF-8 encoding but it seems that it's not working.. Here is what I've so far..

public function createXML($file = 'store.xml', $products){
    if(strpos($file, "xml") === FALSE){
        $file .= ".xml";
    }
    $doc = new DOMDocument('1.0', 'utf-8'); 
    $doc->formatOutput = true; 
    $r = $doc->createElement( "Products" ); 
    $doc->appendChild( $r ); 

    foreach( $products as $product ) 
    {
        $b = $doc->createElement( "Product" ); 
        foreach($product as $key => $value){ 
            if($value !== "Picture"){
                $node = $doc->createElement($key); 
                $node->appendChild($doc->createTextNode((utf8_encode(trim($value))))); 
                $b->appendChild( $node );
            }else{
                $pictures = $doc->createElement("Picuters");
                foreach($value as $pic){
                    $node = $doc->createElement("Picture"); 
                    $node->appendChild($doc->createTextNode((utf8_encode(trim($pic)))));
                    $pictures->appendChild($node);
                }
                $b->appendChild($pictures);
            }
        }
        $r->appendChild( $b );

    } 
    $doc->save($file);
}

But it is not saving data as I want it to..

data in the file is something like this..

<?xml version="1.0" encoding="utf-8"?>
<Products>
  <Product>
    <Brand>Milla by trendyol</Brand>
    <ProductCode>Bluz</ProductCode>
    <ProductName>Güpür Detaylı Bordo</ProductName>
    <ProductURL>http://www.trendyol.com/Gupur-Detayli-Bordo-Bluz/UrunDetay/29920/8562520</ProductURL>
    <ProductStatus>Yes</ProductStatus>
    <Category>Bluz</Category>
    <Gender>Kadın</Gender>
    <OldPrice>69.99</OldPrice>
    <Unit>TL</Unit>
    <NewPrice>49.99</NewPrice>
    <Picture>http://www.trendyol.com/http://s.trendyol.com/Assets/ProductImages/29043/T00400SV6A001_1_org.jpg</Picture>
    <Tags>Güpür Detaylı Bordo, Güpür, Detaylı, Bordo, Butik,Kadin,Luks &amp; Tasarim,Ayakkabi &amp; canta,Milla by trendyol,Women</Tags>
    <EndDate>29.12.2014 22:00:00</EndDate>
  </Product>
</Products>

Like Gender

<Gender>Kadın</Gender>

it should be like

<Gender>Kadïn</Gender>

and other stuff likewise.

Please help....

Thanks.

Upvotes: 0

Views: 4577

Answers (1)

axiac
axiac

Reputation: 72177

Make sure your input data is not already encoded as UTF-8 because if it is, you are double-encoding it by calling utf8_encode(). If you expect to encounter strings encoded as UTF-8 and also using other charsets (ISO-8859-9, I guess) then I think it's better to replace utf8_encode() with a function like this:

function encode_to_utf8_if_needed($string)
{
    $encoding = mb_detect_encoding($string, 'UTF-8, ISO-8859-9, ISO-8859-1');
    if ($encoding != 'UTF-8') {
        $string = mb_convert_encoding($string, 'UTF-8', $encoding);
    }
    return $string;
}

As the documentation says, function utf8_encode() encodes an ISO-8859-1 string to UTF-8. It will not produce the desired results with strings already encoded as UTF-8 or using a different charset.

Upvotes: 3

Related Questions