ParisNakitaKejser
ParisNakitaKejser

Reputation: 14839

new line after xml element in php

I have build a large scale XML writer to build large XML files, but i have a single issue right now, when the file its done i can open the file on my OS X and on Linux disto but Windows computers have big problems to open in eg. notepad.

i think its because my script build XML on a single linje and not split the elements op.

my code look like this.

$filePath = 'file.xml';

$xmlWriter = new XMLWriter();
$xmlWriter->openMemory();
$xmlWriter->startDocument('1.0', 'ISO-8859-1');

$xmlWriter->startElement('rss');
$xmlWriter->writeAttribute('version', '2.0'); 

$xmlWriter->startElement('channel');

if ( $rowProduct !== false )
{
    foreach( $rowProduct AS $key => $val )
    {
        $i++;

        $xmlWriter->startElement('item');

        $xmlWriter->startElement('id');
        $xmlWriter->text('content');
        $xmlWriter->endElement();

        $xmlWriter->endElement();

        if (0 == $i%1000)
        {
            echo "--- clear memery after 1000 rows and dump to xml file ------\n";
            file_put_contents($filePath, $xmlWriter->flush(true), FILE_APPEND);
        }
    }
}

$xmlWriter->endElement();
$xmlWriter->endElement();

file_put_contents($filePath, $xmlWriter->flush(true), FILE_APPEND);

And what i want is xml like this

<xml>
    <root>
        <product>
            <id>content</id>
        </product>
    </root>
</xml>

and not like this

<xml><root><product><id>content</id></product></root></xml>

Can somebody explain what i do wrong here?

Upvotes: 2

Views: 191

Answers (1)

Siim Kallari
Siim Kallari

Reputation: 861

There is a function for this:

http://php.net/manual/en/function.xmlwriter-set-indent.php

I added it to here:

$xmlWriter->startElement('rss');
$xmlWriter->writeAttribute('version', '2.0');
$xmlWriter->setIndent(true);

I cant test it in Notepad, but in Atom it worked.

Upvotes: 2

Related Questions