Search code examples
phpfieldseparatorxmlwriter

Change XMLWriter Default Field Separator


I'm am looking to retrieving a bucketload of data from a Site, selecting specific sections of it, turning it into an XML Format, then importing it into a database (This question is about the XML Format Section)

the data received is a Raw Text file, that is distinguished into many different categories, however. The Field Separator used by this file is ":" and from my current experience, XMLWriter likes the Separator to be ","

is there any way I can change the Separator XMLWriter uses?

Current XML Code:

    <?php
$data = fopen('thing.txt', 'r');

    $xml = new XMLWriter;
    $xml->openURI('php://output');
    $xml->setIndent(true); // makes output cleaner

    $xml->startElement('Controllers');
    while ($line = fgetcsv($data)) {
       $xml->startElement('Current');
       $xml->writeElement('Callsign', $line[0]);
       $xml->writeElement('CID', $line[1]);
       $xml->writeElement('Name', $line[2]);
       $xml->endElement();
    }
    $xml->endElement();

Thanks!


Solution

  • XMLWriter is the part which creates the output, it doesn't know anything about the format of the file your reading. This will be down to the call to fgetcsv(). The third parameter to which is the delimiter you want to use, so change the read line to...

    while ($line = fgetcsv($data, 0, ':')) {