Reputation: 97
I have a CSV file.
Quotes.csv
quote|source|dob-dod|wplink|wpimg|category
There is no remedy but to love more.|Henry David Thoreau|1817-1862|http://en.wikipedia.org/wiki/Henry_David_Thoreau|http://upload.wikimedia.org/wikipedia/commons/f/f0/Benjamin_D._Maxham_-_Henry_David_Thoreau_-_Restored.jpg|romantic
There is always some madness in love. But there is also always some reason in madness.|Friedrich Nietzsche|1844-1900|http://en.wikipedia.org/wiki/Friedrich_Nietzsche|http://upload.wikimedia.org/wikipedia/commons/1/1b/Nietzsche187a.jpg|romantic
Love does not consist in gazing at each other, but in looking outward together in the same direction.|Antoine de Saint-Exupery|1900-1944|http://en.wikipedia.org/wiki/Antoine_de_Saint-Exupery|http://upload.wikimedia.org/wikipedia/commons/7/7f/11exupery-inline1-500.jpg|romantic
Take away love and our earth is a tomb.|Robert Browning|1812-1889|http://en.wikipedia.org/wiki/Robert_Browning|http://upload.wikimedia.org/wikipedia/commons/5/58/Robert_Browning_by_Herbert_Rose_Barraud_c1888.jpg|humerous
Work is the curse of the drinking classes.|Oscar Wilde|1854-1900|http://en.wikipedia.org/wiki/Oscar_wilde|http://upload.wikimedia.org/wikipedia/commons/a/a7/Oscar_Wilde_Sarony.jpg|humerous
A computer once beat me at chess, but it was no match for me at kick boxing.|Emo Philips|1956-|http://en.wikipedia.org/wiki/Emo_Philips|http://upload.wikimedia.org/wikipedia/commons/7/76/Emo_Philips_2002_cropped.jpg|humerous
Women should be obscene and not heard.|Groucho Marx|1890-1977|http://en.wikipedia.org/wiki/Groucho_Marx|http://upload.wikimedia.org/wikipedia/commons/6/68/Groucho_Marx_-_portrait.jpg|humerous
An ideal form of government is democracy tempered with assassination.|Voltaire|1694-1778|http://en.wikipedia.org/wiki/Voltaire|http://upload.wikimedia.org/wikipedia/commons/c/c2/D%27apr%C3%A8s_Maurice_Quentin_de_La_Tour%2C_Portrait_de_Voltaire%2C_d%C3%A9tail_du_visage_%28ch%C3%A2teau_de_Ferney%29.jpg|political
People have only as much liberty as they have the intelligence to want and the courage to take.|Emma Goldman|1869-1940|https://en.wikipedia.org/wiki/Emma_Goldman|https://upload.wikimedia.org/wikipedia/commons/0/03/Emma_Goldman_seated.jpg|political
Religion is what keeps the poor from murdering the rich.|Napoleon Bonaparte|1769-1821|http://en.wikipedia.org/wiki/Napoleon|http://upload.wikimedia.org/wikipedia/commons/5/50/Jacques-Louis_David_-_The_Emperor_Napoleon_in_His_Study_at_the_Tuileries_-_Google_Art_Project.jpg|political
Any dictator would admire the uniformity and obedience of the [U.S.] media.|Noam Chomsky|1928-|http://en.wikipedia.org/wiki/Noam_Chomsky|https://upload.wikimedia.org/wikipedia/commons/a/a9/Noam_Chomsky_portrait_2017.jpg|political
Isn't it ironic that the proprietary software developers call us communists? We are the ones who have provided for a free market, where they allow only monopoly. ... if the users chooses this proprietary software package, he then falls into this monopoly for support ... the only way to escape from monopoly is to escape from proprietary software, and that is what the free software movement is all about. We want you to escape and our work is to help you escape. We hope you will escape to the free world. The free world is the new continent in cyberspace that we have built so we can live here in freedom. It's impossible to live in freedom in the old world of cyberspace, where every program has its feudal lord that bullies and mistreats the users. So, to live in freedom we have to build a new continent. Because this is a virtual continent, it has room for everyone, and there are no immigration restrictions. And because there were never indigenous peoples in cyberspace, there is also no issue of taking away their land. So everyone is welcome in the free world, come to the free world, live with us in freedom. The free software movement aims for the liberation of cyberspace and everyone in it.|Richard Stallman|1953-|http://en.wikipedia.org/wiki/Richard_Stallman|https://upload.wikimedia.org/wikipedia/commons/2/28/Richard_Stallman_at_LibrePlanet_2019.jpg|political
The paradise of the rich is made out of the hell of the poor.|Victor Hugo|1802-1885|https://en.wikipedia.org/wiki/Victor_Hugo|https://upload.wikimedia.org/wikipedia/commons/e/e6/Victor_Hugo_by_%C3%89tienne_Carjat_1876_-_full.jpg|political
I am trying to write a php script to convert this to xml, however am currently getting this error
Uncaught DOMException: Invalid Character Error
on this line:
$child = $doc->createElement(trim($header));
Here is my current php code, how do I get this script working?
<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);
ini_set('auto_detect_line_endings', true);
$inputFilename = 'quotes.csv';
$outputFilename = 'test.xml';
// Open csv to read
$inputFile = fopen($inputFilename, 'rt');
// Get the headers of the file
$headers = fgetcsv($inputFile);
// Create a new dom document with pretty formatting
$doc = new DomDocument();
$doc->formatOutput = true;
// Add a root node to the document
$root = $doc->createElement('rows');
$root = $doc->appendChild($root);
// Loop through each row creating a <row> node with the correct data
while (($row = fgetcsv($inputFile)) !== FALSE)
{
$container = $doc->createElement('row');
foreach($headers as $i => $header)
{
$child = $doc->createElement(trim($header));
$child = $container->appendChild($child);
$value = $doc->createTextNode($row[$i]);
$value = $child->appendChild($value);
}
$root->appendChild($container);
}
$strxml = $doc->saveXML();
?>
Target Output
<?xml version="1.0" encoding="UTF-8"?>
<quotes>
<record id="1">
<quote>There is no remedy but to love more.</quote>
<source>Henry David Thoreau</source>
<dob-dod>1817-1862</dob-dod>
<wplink>http://en.wikipedia.org/wiki/Henry_David_Thoreau</wplink>
<wpimg>http://upload.wikimedia.org/wikipedia/commons/f/f0/Benjamin_D._Maxham_-_Henry_David_Thoreau_-_Restored.jpg</wpimg>
<category>romantic</category>
</record>
<record id="2">
<quote>There is always some madness in love. But there is also always some reason in madness.</quote>
<source>Friedrich Nietzsche</source>
<dob-dod>1844-1900</dob-dod>
<wplink>http://en.wikipedia.org/wiki/Friedrich_Nietzsche</wplink>
<wpimg>http://upload.wikimedia.org/wikipedia/commons/1/1b/Nietzsche187a.jpg</wpimg>
<category>romantic</category>
</record>
<record id="3">
<quote>Love does not consist in gazing at each other, but in looking outward together in the same direction.</quote>
<source>Antoine de Saint-Exupery</source>
<dob-dod>1900-1944</dob-dod>
<wplink>http://en.wikipedia.org/wiki/Antoine_de_Saint-Exupery</wplink>
<wpimg>http://upload.wikimedia.org/wikipedia/commons/7/7f/11exupery-inline1-500.jpg</wpimg>
<category>romantic</category>
</record>
<record id="4">
<quote>Take away love and our earth is a tomb.</quote>
<source>Robert Browning</source>
<dob-dod>1812-1889</dob-dod>
<wplink>http://en.wikipedia.org/wiki/Robert_Browning</wplink>
<wpimg>http://upload.wikimedia.org/wikipedia/commons/5/58/Robert_Browning_by_Herbert_Rose_Barraud_c1888.jpg</wpimg>
<category>humerous</category>
</record>
<record id="5">
<quote>Work is the curse of the drinking classes.</quote>
<source>Oscar Wilde</source>
<dob-dod>1854-1900</dob-dod>
<wplink>http://en.wikipedia.org/wiki/Oscar_wilde</wplink>
<wpimg>http://upload.wikimedia.org/wikipedia/commons/a/a7/Oscar_Wilde_Sarony.jpg</wpimg>
<category>humerous</category>
</record>
<record id="6">
<quote>A computer once beat me at chess, but it was no match for me at kick boxing.</quote>
<source>Emo Philips</source>
<dob-dod>1956-</dob-dod>
<wplink>http://en.wikipedia.org/wiki/Emo_Philips</wplink>
<wpimg>http://upload.wikimedia.org/wikipedia/commons/7/76/Emo_Philips_2002_cropped.jpg</wpimg>
<category>humerous</category>
</record>
<record id="7">
<quote>Women should be obscene and not heard.</quote>
<source>Groucho Marx</source>
<dob-dod>1890-1977</dob-dod>
<wplink>http://en.wikipedia.org/wiki/Groucho_Marx</wplink>
<wpimg>http://upload.wikimedia.org/wikipedia/commons/6/68/Groucho_Marx_-_portrait.jpg</wpimg>
<category>humerous</category>
</record>
<record id="8">
<quote>An ideal form of government is democracy tempered with assassination.</quote>
<source>Voltaire</source>
<dob-dod>1694-1778</dob-dod>
<wplink>http://en.wikipedia.org/wiki/Voltaire</wplink>
<wpimg>http://upload.wikimedia.org/wikipedia/commons/c/c2/D%27apr%C3%A8s_Maurice_Quentin_de_La_Tour%2C_Portrait_de_Voltaire%2C_d%C3%A9tail_du_visage_%28ch%C3%A2teau_de_Ferney%29.jpg</wpimg>
<category>political</category>
</record>
<record id="9">
<quote>People have only as much liberty as they have the intelligence to want and the courage to take.</quote>
<source>Emma Goldman</source>
<dob-dod>1869-1940</dob-dod>
<wplink>https://en.wikipedia.org/wiki/Emma_Goldman</wplink>
<wpimg>https://upload.wikimedia.org/wikipedia/commons/0/03/Emma_Goldman_seated.jpg</wpimg>
<category>political</category>
</record>
<record id="10">
<quote>Religion is what keeps the poor from murdering the rich.</quote>
<source>Napoleon Bonaparte</source>
<dob-dod>1769-1821</dob-dod>
<wplink>http://en.wikipedia.org/wiki/Napoleon</wplink>
<wpimg>http://upload.wikimedia.org/wikipedia/commons/5/50/Jacques-Louis_David_-_The_Emperor_Napoleon_in_His_Study_at_the_Tuileries_-_Google_Art_Project.jpg</wpimg>
<category>political</category>
</record>
<record id="11">
<quote>Any dictator would admire the uniformity and obedience of the [U.S.] media.</quote>
<source>Noam Chomsky</source>
<dob-dod>1928-</dob-dod>
<wplink>http://en.wikipedia.org/wiki/Noam_Chomsky</wplink>
<wpimg>https://upload.wikimedia.org/wikipedia/commons/a/a9/Noam_Chomsky_portrait_2017.jpg</wpimg>
<category>political</category>
</record>
<record id="12">
<quote>Isn't it ironic that the proprietary software developers call us communists? We are the ones who have provided for a free market, where they allow only monopoly. ... if the users chooses this proprietary software package, he then falls into this monopoly for support ... the only way to escape from monopoly is to escape from proprietary software, and that is what the free software movement is all about. We want you to escape and our work is to help you escape. We hope you will escape to the free world. The free world is the new continent in cyberspace that we have built so we can live here in freedom. It's impossible to live in freedom in the old world of cyberspace, where every program has its feudal lord that bullies and mistreats the users. So, to live in freedom we have to build a new continent. Because this is a virtual continent, it has room for everyone, and there are no immigration restrictions. And because there were never indigenous peoples in cyberspace, there is also no issue of taking away their land. So everyone is welcome in the free world, come to the free world, live with us in freedom. The free software movement aims for the liberation of cyberspace and everyone in it.</quote>
<source>Richard Stallman</source>
<dob-dod>1953-</dob-dod>
<wplink>http://en.wikipedia.org/wiki/Richard_Stallman</wplink>
<wpimg>https://upload.wikimedia.org/wikipedia/commons/2/28/Richard_Stallman_at_LibrePlanet_2019.jpg</wpimg>
<category>political</category>
</record>
<record id="13">
<quote>The paradise of the rich is made out of the hell of the poor.</quote>
<source>Victor Hugo</source>
<dob-dod>1802-1885</dob-dod>
<wplink>https://en.wikipedia.org/wiki/Victor_Hugo</wplink>
<wpimg>https://upload.wikimedia.org/wikipedia/commons/e/e6/Victor_Hugo_by_%C3%89tienne_Carjat_1876_-_full.jpg</wpimg>
<category>political</category>
</record>
</quotes>
Upvotes: 1
Views: 41
Reputation: 57121
You need to make sure the when you read the file, you use the right separator, in your case a |
. This is the third parameter to fgetcsv
...
$headers = fgetcsv($inputFile, null, "|");
and
while (($row = fgetcsv($inputFile, null, "|")) !== FALSE)
Upvotes: 1