Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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
    New Coder
    Join Date
    Apr 2008
    Posts
    31
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Since you are asking in a php forum, will give answer it with php in mind

    You can use something like this:
    PHP Code:
    if (($handle fopen("source.csv""r")) !== FALSE) {

        while ((
    $data fgetcsv($handle1000",")) !== FALSE) {

              echo 
    $data[0];
              echo 
    $data[1];
              echo 
    $data[2];
              
    // etc

            
    }

    On second study it seems your csv has an opposite direction of what i would expect, so this example would not give the results expected, not entirely sure how to get the results you want, as you would need to rebuild the array entirely to do what you wish with it.

    EDIT (again)
    Something like this should work.
    PHP Code:
    $row 0;
    while ((
    $data fgetcsv($handle1000",")) !== FALSE) {

        
    // edit for number of horisontal fields
        
    $data2["person1"][$row] = $data[0];
        
    $data2["person2"][$row] = $data[1];
        
    $data2["person3"][$row] = $data[2];

    $row=$row+1;

    And then you can work with that something similar to:
    PHP Code:
    echo "<root>";
        for (
    $i=0;$i<sizeof($data2);$i++) {
            
            echo 
    "<person".$i.">";



          echo 
    "<name>"$data2["person".$i][1]."</name>";
          echo 
    "<lastname>"$data2["person".$i][2]."</lastname>";

            echo 
    "</person".$i.">";
            }
    echo 
    "</root>";

    Which should if all is well give something similar to the desires result..

    You probably want to make a while within the while to convert the array, as its a dynamic thing, but this should give some some idea.

    Good luck
    Last edited by Jilli; 12-07-2013 at 03:35 AM.

  • #3
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,683
    Thanks
    25
    Thanked 656 Times in 655 Posts
    @saywhatyousee, welcome to the forums, hope it's not the only time you'll come here.
    On to your problem. You really don't need the person tag as person1,person2 - etc. person works great in an XML file.
    If your csv file is not written in stone you'll find it a lot easier to have your file set up like this:
    Code:
    person,first,last
    john,doe
    steve,whinney
    mike,speirs
    This way it's easy to add names and increase the amount of data for each person.

    What do you think?
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #4
    New Coder
    Join Date
    Apr 2008
    Posts
    31
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Agreed, it's really an awkward format to work with, can just use my first block of code if it is formatted like sunfighter suggested, without needing to rebuild the array. Which makes everything a whole lot easier.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •