Jump to content

Csv To Xml File


real_illusions

Recommended Posts

Hi all,Is there an easy to convert an uploaded CSV file to an XML file for inclusion to a database?Initially the idea was to insert a csv file into the database but having problems with comma's inside the data, so thought maybe an xml file would be easier.I've looked around but cant find much in the way of scripts to convert csv to xml, and those that ive found dont appear to work.Thanks:)

Link to comment
Share on other sites

It does neither, it generates a CSV file so that it's correct in the first place. If your CSV file is correct then you can import it into a database without converting it to XML. I may be confused about your problem, but it sounds like your solution is to generate a correct CSV file, instead of trying to convert an incorrect CSV file to an XML file.

Link to comment
Share on other sites

It's not really going to be possible to determine which comma needs to be escaped. CSV data should be quoted, that way if a comma is inside a quoted string you know it's part of the data and not another field. It's going to be difficult to write an algorithm to go through a CSV file and fix it, it's hard to determine which commas are part of the data and which are part of the structure. That's why I suggested to just create the CSV in a correct format to begin with. If you're not the one creating the CSV then there's not a lot you can do, if someone is creating a CSV file and not bothering to quote the fields you can't really do much.

Link to comment
Share on other sites

You can turn any string into a proper string for inclusion into a database by the use of mysql_real_escape_string() (or mysqli_real_escape_string() if you are lucky enough to use MySQLi).It's actually extremely highly reccomended (no, think of it as mandatory!!!) that you use this function over any string that is to be entered into the DB, for the sake of preventing SQL injection attacks.

Link to comment
Share on other sites

Thx for the replies, but i dont think the mysql_real_escape_string() string is what I'm after. Yes it escapes the " and ' but not the commas.Wrapping the data in " or ' the commas still are read as the end of the line.If it helps, this small bit of code is where I'm splitting the CSV file to seperate the data.$line = trim($contents[$i],'",');$arr = explode(',', $line);Is there a way to tell it skip over any commas that are within " or ' ??Or a way to seperate the rows/data by column headers rather than commas?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...