Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8423863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:50:36+00:00 2026-06-10T03:50:36+00:00

I have a csv file which I want to convert to xml but the

  • 0

I have a csv file which I want to convert to xml but the CSV is encoded as ANSI and when the xml file is generated all chars which are special like: áéőúűóüöí are looking like this: ó ízű

And than there are html tags which are messing up the whole XML.
I tried to add strip_tags but that didn’t work
And now I have empty nodes between products and the HTML codes are all over the XML and not between node
Oh and because of the CSV I had to put an array_pop() to remove the extra semicolon at the end of each line.

My CSV looks like this:

title;seller_product_id;status;price;discount_price;warranty;manufacturer;weight;category;disable_shipping;photo_url_1;photo_url_2;photo_url_3;photo_url_4;seller_category;video_link;isbn13;gtin;product_code;warehouse_code;page_link;availability_237;shipping_fee_4_1;shipping_fee_4_2;description;
"tea csipkebogyó ízű 20*1,5g..";"5999881069013";1;"121";;1;;"60";8510;0;"http://mydomain/00003125_mid.jpg";"http://mydomain/00001634_mid.jpg";;;"Élelmiszer; Tea";;;;"5999881069013";"000000019231";" http://mydomain/producturl";"1744";;;<table width="975" cellspacing="0" cellpadding="0"><tbody><tr><td height="151" valign="top" style="background: url('http://mydomain/vlshop.PNG') no-repeat" colspan="2"></td></tr><tr><td style="font-family: Arial,Helvetica,sans-serif" colspan="2"><div style="text-align: justify"><div style="text-align: center"><span style="color: rgb(255, 0, 0)"><span style="font-size: large"><strong>Több termék vásárlása esetén használja a kosaras vásárlást!</strong></span></span></div><span style="color: rgb(255, 0, 0)"><span style="font-size: medium"><strong><br /><br />tea csipkebogyó ízű 20*1,5g..<br /><br />Cikkszám: 000000019231<br /><br />STAMFORD tea csipkebogyó ízű 20*1,5g..<br /><br />Csak új és garanciális termékeket árusítunk!<br />Csere és beszámítás nem lehetséges!<br /><br /><span style="font-size: large">Szállítási és fizetési információk:<br /></span><br />Lehetőség van a termékeket személyesen is átvenni:<br />- Munkanapokon 8-16 óráig: Szada, (Pest Megye)! </strong></span></span><span style="color: rgb(255, 0, 0)"><span style="font-size: medium"><strong><br /><br />Legalacsonyabb szállítás díjunk 660 Ft, ajánlott postázásra nincs lehetőség!<br /><br />Futárszolgálati díjaink előreutalás esetén:<br />- 0kg-2kg: 660 Ft<br />- 2kg-5kg: 800 Ft<br />- 5kg-30kg: 1200 Ft<br />- Utánvét +:  150 Ft<br /><br />A reggel 8 óráig beérkezett rendeléseket tudjuk aznap feldolgozni!<br />A fizetéssel és az átvétellel kapcsolatos igényeit kérjük tüntesse fel a megjegyzés rovatban!<br /><br /><br /></strong></span></span></div></td></tr></tbody></table>;

and I’m using this to create the XML:

<?php

error_reporting(E_ALL ^ E_NOTICE);
ini_set("display_errors", true);
function csv2xml($file, $container = 'data', $rows = 'row')
{
$r = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
$r .= "<{$container}>\n";
$row = 0;
$cols = 0;
$titles = array();
$handle = @fopen($file, 'r');
if (!$handle) return $handle;
while (($data = fgetcsv($handle, 1000, ';')) !== FALSE)
{
array_pop($data);
if ($row > 0) $r .= "\t<{$rows}>\n";
if (!$cols) $cols = count($data);
for ($i = 0; $i < $cols; $i++)
{
if ($row == 0)
{
$titles[$i] = $data[$i];
continue;
}
//print_r($data[$i]);
$r .= "\t\t<{$titles[$i]}>";
$r .= strip_tags(str_replace('; ', '/', $data[$i]));
$r .= "</{$titles[$i]}>\n";
}
if ($row > 0) $r .= "\t</{$rows}>\n";
$row++;
}
fclose($handle);
$r .= "</{$container}>";
return $r;
}
$xml = csv2xml('feed.csv', 'products', 'product');
$xmlfile = @fopen('test.xml', 'wb') or die('Could not open XML file for writing');
fwrite($xmlfile, $xml) or die('Could not write string to XML file');
fclose($xmlfile);
echo "Successfully wrote the XML file";

?>

Oh and can’t edit the original CSV because I got the file from an external url.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:50:38+00:00Added an answer on June 10, 2026 at 3:50 am

    I solved it 🙂
    I had to increase the the length parameter because the lines were longer than 1000.
    so my solution is this

    while (($data = fgetcsv($handle, 4000, ';')) !== FALSE) {...}
    

    And somehow this solved the character encoding problem too.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a daily-generated CSV file containing contact data which I want load entries
I have a csv file which may have empty or blank rows. I want
I have a csv format file, which I want to import to sql server
I have a csv file which is something like this: book_store_id book_price name 1
I have a csv-file which i want to transform with fmpp (freemarker). The first
I have a csv file which contains the numbers of buttons. I want to
I want to write a Object into CSV file. For XML we have XStream
I have a csv File which is of course comma separated and want to
Little Background: I have csv file which has lots of rows and each row
I have a CSV file which has the following format: id,case1,case2,case3 Here is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.