xml to csv, csv to xml

I want to be able to convert xml files into csv files, alter the csv file with code I have written and then convert the csv file back to xml.   I know there is a bunch of classes already written   ie pear.  Problem is I'm not sure where to start.   Do I need pears XML_Parser, the XML_RSS  parser, XML_Serializer.   Do I need one for xml to csv and another for csv to xml.
Any input appreciated, don't want to waste my time barking up the wrong tree.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Can you give a sample XML file, and a Sample CSV file.

I personally suggest you edit the XML File directly using the DOM_XML functions.
Having that said, if you do want to convert between the two, that you use XSLT for xml->csv, and simply iterate through the values for visa-vearsa.
dplinnaneAuthor Commented:
Here is the xml file


I would like to be able to place this in an array
$array_val    = array($column_header=>$data);

$data  is the data associated with each column.

Once I do this I can format the data and then either convert it to csv or back to xml.

I would alos like to be able to do the reverse, convert this to an array and the to xml.

Basically once I get an array I can do whatever I want xml or csv.   I have been looking at XML_serialiser on pear.php
there is also a class called PARSER which is based on xpath.

I know nothingabout xslt so I am limited there.   Any further inputwould be appreciated, I will look at DOM_XML and see can I make any sense of that.

"(512) 257-2821","$6,050.00","Thursday, December, 04, 2003"
"(512) 257-2821","$3,700.00","Thursday, December, 04, 2003"
"(512) 257-2821","$0.00","Thursday, December, 04, 2003"
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

If you have the DOMXML extensions enabled, this is easy:

  /* XML to CSV */

  $csv = array();

  $doc = domxml_open_file( realpath("myfile.xml") );

  $rows = $doc->get_elements_by_tagname("row");
  echo count($rows) . "<br>";

  foreach($rows as $row)
    $row_elements = $row->child_nodes();
    echo count($row_elements) . "<br>";
    $temp = array();
    foreach($row_elements as $row_element)
      $tag = $row_element->tagname();
      $text_node = $row_element->first_child();
      $text = $text_node->get_content();
      $temp[$tag] = $text;
    $csv[] = $temp;
  echo "<pre>";
  $output = "";
  foreach($csv as $item)
    $output .= implode(",", array_values($item)) . "\n";
  $output = substr($output, 0, -1);
  echo $output;
  echo "</pre>";
  $fp = fopen("myfile.csv", "w");
  fwrite($fp, $output);

  /* CSV TO XML */

  $el_names = array("REGISTRATION_NUM", "SERIAL_NUM", "PHONE_NUM");

  $fp = fopen("myfile.csv", "r");

  $doc = domxml_new_doc("1.0");
  $data = $doc->create_element("data");

  while( !feof($fp) )
    $row = $doc->create_element("row");
    $row_els= array();
    $items = explode(",", fgets($fp, 512));

    for($i = 0; $i < count($el_names); $i++)
      $el_name = $el_names[$i];
      $row_els[$el_name] = $doc->create_element($el_name);

      $item = $doc->create_text_node($items[$i]);




  echo "<pre>" . htmlspecialchars( $doc->dump_mem(TRUE) ) . "</pre>";
  $doc->dump_file(realpath("new-file.xml"), TRUE, FALSE);

Enabling DOMXML for PHP on Windows is also quite easy. Holler if you need a hand with it.

Note: DOMXML was overhauled for PHP 4.3.0 -- don't waste your time using the earlier versions of this extension, they're not W3C DOM compliant.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Oops, I forgot to do the bit with the column headings -- if you can't modify what I've givien you to take care of that, give me a shout and I'll lend a hand with that also.
dplinnaneAuthor Commented:
Thanks, This looks great. I can't test because I cannot get domxml going on windows.   I tried the following.

Copied iconv.dll from the C:/PHP/dlls folder to your C:/WINNT/SYSTEM32 folder.   I followed instructions from http://us2.php.net/domxml.
Do they mean move that file or copy it?  I restarted apache but mo luck. Any ideas. Thx

Call to undefined function: domxml_open_file() in C:\Program Files\Apache Group\Apache2\htdocs\body\body_test_execute.php on line 628
Did you copy php_domxml.dll to the system directory as well?

Usually what I do when installing a new PHP distribution on Windows is just to copy everything from the extensions and dll directories to system32. ;)

Don't forget to uncomment the lines referring to php_domxml.dll and iconv.dll in your php.ini, either.
dplinnaneAuthor Commented:
excellent that works.
However, I'm not sure how to get the first part xml to csv to add the column headers, if you could assist I would appreciate it.  One more question, I will be uploading an xml file is there a function to check that the file is in the desired format ie simple xml where there is a column headers and a row of matching data like in the example I have given above. Thx.
Just output a line with the column headers, since you already know what they're supposed to be. Just change

$output = "";



or however you want them to read.

If you want to validate the XML, you'll need to write a DTD to validate it against.

Are you saying is that what you're after after is a more generic solution?
Richard QuadlingSenior Software DeveloperCommented:
You could ignore XML entirely and use plain text or regular expressions to alter the data.

What alterations do you want to do?
dplinnaneAuthor Commented:
Your suggestion will work

but the solution will needs to be dynamic because a user could upload an xml file, with different column names so we need to be able to grab
the first row of data from xml file and use the tag name <REGISTRATION_NUMBER><SERIAL_NUM><PHONE_NUM>. I have added the following code for xml to csv, this seems to work I get echo = REGISTRATION_NUM,SERIAL_NUM,PHONE_NUM, for csv to xml I can easily grab the 1st row which will the col_hdr instead of using $el_names = array("REGISTRATION_NUM", "SERIAL_NUM", "PHONE_NUM");

foreach($row_elements as $row_element)
      $col_hdr          = $row_element->tagname();
      $row_col_hdr[]    = $col_hdr;
     $csv_row_col_hdr = implode(",", $row_col_hdr). "\n";    
     echo "csv_row_col_hdr $csv_row_col_hdr".'<br>';

You could ignore XML entirely and use plain text or regular expressions to alter the data. HAS TO BE XML

What alterations do you want to do? THIS PART IS EASY, the xml->csv  and csv->xml was the difficult part.

Concerning my question on validating an xml file it should always be in the same format. Thanks for your help new to xml just a week so didn't really know where to start.

 I'll close this one out on reply let me know if you want me to post this
"If you want to validate the XML, you'll need to write a DTD to validate it against."
as a new question as you deserve more then 500 points for this one.

Not a clue on where to start with DTD to validate it against.
The basics of writing a DTD are not that hard. Here's links to a couple of tutorials I used a while back to get up to speed:

http://www-106.ibm.com/developerworks/xml/library/x-buildappl/writedtd.html -- this will show you all the basics

http://www.vivtek.com/xml/writing_dtd.html -- not as good as the the one from IBM, but I figured I'd better provide at least one alternative

You can also learn quite a bit from looking at the DTD for a markup language you're doubtless familiar with -- HTML. You can find the HTML DTDs at W3C.org (included with the HTML specifications).

Here's a sample DTD for the original bit of XML you posted:

<!ELEMENT data (row)+>

AS you'll see in the tutoprials, you can also define attributes and entities. For example, if you wanted to use the format

  <row REGISTRATION_NUM="abcde12" SERIAL_NUM="#####" PHONE_NUM="512-555-1234"  />
  <row REGISTRATION_NUM="abcde15" SERIAL_NUM="#####" PHONE_NUM="512-555-2345"  />
  <row REGISTRATION_NUM="abcde27" SERIAL_NUM="#####" PHONE_NUM="512-555-3456"  />

you could use a DTD like this:

<!ELEMENT data (row)+>

You also might want to take a look at XML Schemas, which are basically just a way to write DTDs using XML. (I'm not an expert with XML Schemas -- they're on my Must Learn For Real List, heh.) There's a helpful introduction to these at http://www.w3.org/TR/xmlschema-0/ ... the advantage to using XML Schemas is that you have much more control over datatypes than you do with a DTD.
am trying to do something similar but with a single row and n nuber of variables, what parts of the csv to xml code above should i change to make it work for me ??
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.