0

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.

2
  • How do you read the XML file? Maybe the program you use to read it doesn't recognize the UTF-8 encoding and treats the file as being encoded as ISO-8859-1 or other 1-byte charset. Commented Dec 27, 2014 at 16:58
  • I tried to reopen using Encoding UTF-8, but still the same Commented Dec 27, 2014 at 17:09

1 Answer 1

3

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.

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.