#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0

    How do I convert a csv file to xml in php?


    I have a csv file which contains this data:

    person1,person2,person3
    first,john,steve,mike
    last,doe,whinney,speirs

    I want to create an xml file which looks like this:

    <root>
    <person1>
    <first>
    john
    </first>
    <last>
    doe
    </last>
    </person1>
    <person2>
    <first>
    steve
    </first>
    <last>
    whinney
    </last>
    </person2>
    <person3>
    <first>
    mike
    </first>
    <last>
    speirs
    </last>
    </person3>
    </root>

    Thanks
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,864
    Rep Power
    368
    loop through that file use xmlwriter class to create an xml..
  4. #3
  5. Come play with me!
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,742
    Rep Power
    9397
    What the heck is up with that CSV structure? It's totally messed up.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,864
    Rep Power
    368
    oh my, i didnt obviously see the csv propelry, I thought it would be one line = related data..

    that is not a nice way... and can cause problems
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,910
    Rep Power
    1045
    Looks like it's supposed to be something like this:

    Code:
          │ person1 │ person2 │ person3
    ══════╪═════════╪═════════╪═════════
    first │ john    │ steve   │ mike
    last  │ doe     │ whinney │ speirs
    Don't let Excel people mess with your data!

    Fixing this will be ugly -- if your CSV is even valid. You'll first have to go through the whole CSV and put the data into a temporary array in order to untangle the structure. And then you have to go through the array to create your XML.

    The correct format for the CSV would have been this:

    Code:
    first,last
    john,doe
    steve,whinney
    mike,speirs
    If the "person1," "person2", ... stuff is important, you'd put it into an additional column.
    Last edited by Jacques1; December 7th, 2013 at 05:23 AM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    China
    Posts
    21
    Rep Power
    0
    Unless there's a good reason for that structure, surely this would be more rational?

    person, first, last
    1, john, doe
    2, steve, whinney
    3, mike, speirs

    Also much easier to convert to XML.

    -Ifor
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,910
    Rep Power
    1045
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Location
    China
    Posts
    21
    Rep Power
    0
    Originally Posted by Jacques1
    Look at the post above yours.


    Oops, and you even put them in code tags. I guess you win this round... haha

IMN logo majestic logo threadwatch logo seochat tools logo