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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.