www.webdeveloper.com
Results 1 to 2 of 2

Thread: XML question

  1. #1
    Join Date
    May 2008
    Posts
    16

    XML question

    Hey I have this csv2xml parser

    <?php
    /**
    * Converts a CSV file to a simple XML file
    *
    * @param string $file
    * @param string $container
    * @param string $rows
    * @return string
    */

    error_reporting(E_ALL ^ E_NOTICE);
    ini_set("display_errors", true);


    function csv2xml($file, $container = 'data', $rows = 'row')
    {

    $r = "<{$container}>\n";
    $row = 0;
    $cols = 0;
    $titles = array();

    $handle = @fopen($file, 'r');
    if (!$handle) return $handle;

    while (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
    {
    if ($row > 0) $r .= "\t<{$rows}";
    if (!$cols) $cols = count($data);
    for ($i = 0; ($i < $cols) && (strlen($data[$i]) > 0); $i++)
    {
    if ($row == 0)
    {
    $titles[$i] = $data[$i];
    continue;
    }

    if ($titles[$i] == "Description")
    {
    $r .= ">\n\t\t<description><![CDATA[";
    $r .= $data[$i];
    $r .= "]]></description>\n";
    }
    elseif ($titles[$i] == "ID")
    $r .= " adID=\"{$data[$i]}\"";
    elseif ($titles[$i] == "Category")
    $r .= " catID=\"{$data[$i]}\"";
    }
    if ($row > 0) $r .= "\t</{$rows}>\n";
    $row++;
    }
    fclose($handle);
    $r .= "</{$container}>";

    return $r;


    }



    $xml = csv2xml('dtifeed.csv', 'export', 'ad');

    $xmlfile = @fopen('dtifeed.xml', 'wb') or die('Could not open XML file for writing');

    fwrite($xmlfile, $xml) or die('Could not write string to XML file');

    fclose($xmlfile);

    echo "Successfully wrote the XML file";






    ?>
    it works fine adn generate xml out put the thing is that I need to add
    this to the top of the xml <xml version="1.0" encoding="Windows-1252">

    Can some help with this?

    Thank you

  2. #2
    Join Date
    May 2008
    Posts
    381
    Change:
    Code:
    $r = "<{$container}>\n";
    to:
    Code:
    $r = "<?xml version="1.0" encoding="Windows-1252"?>\n<{$container}>\n";
    That should be all that is needed. ^_^

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles