Jump to content

XML from CSV with recurring, unsorted values


subarashi78

Recommended Posts

Hi guys,

 

long-time reader here and finally have my own question.

I have a MySql db from which I get a CSV file that I need in XML.

 

The problem is, the parent nodes appear multiple times in the CSV and so do other values.

 

Let me give you a quick overview of what the data I have looks like, and the XML of what I need to achieve as output.

 

CSV:

 

FR,FRANCE,CITY002,A,CITY000,B,CITY001,C,CODE1,NAME1

DE,GERMANY,CITY004,D,NULL,NULL,NULL,NULL,CODE2,NAME2

FR,FRANCE,CITY003,E,CITY000,B,CITY002,A,CODE3,NAME3

 

XML:

 

<countries>

<country code="FR" name="FRANCE">

<city name="A" code="CITY002">

<hotel name="NAME1" code="CODE1" />

<hotel name="NAME3" code="CODE3"/>

</city>

<city name="B" code="CITY000">

<hotel name="NAME1" code="CODE1" />

<hotel name="NAME3" code="CODE3"/>

</city>

<city name="C" code="CITY001">

<hotel name="NAME1" code="CODE1" />

</city>

<city name="E" code="CITY003">

<hotel name="NAME3" code="CODE3"/>

</city>

</country>

<country code="DE" name="GERMANY">

<city name="E" code="CITY003">

<hotel name="NAME2" code="CODE2" />

</city>

</country>

</countries>

 

Now I know it looks kinda complicated because the context isn't immediately obvious, but if it wasn't, I wouldn't be posting here  ;)

 

I started using file() to get each line into an array and then used explode() in a loop to generate new arrays.

The only thing where I am struggling is going back and adding cities to countries but not duplicating them. If a city (or country) already exists, then I should just add to the parent node.

 

Not sure if anyone even understands this as, reading over it again, not even I do but if you have any questions that may clarify the issue, let me know. I'm probably just looking for a way to create a multidimensional arrays, using the parent node as an index or something like that.

 

Thanks,

 

Daniel

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.