I have few files in .txt format (these files are either saved in a folder or opened from the desktop after execution).
I would like to build an application in PHP that can retrieve these files and save them in XML format and then display then dynamically with XSLT.
Are there any methods that will allow me to do this?
This is the code I have up to now:
$fp = fopen('optim.txt', 'r');
$xml = new XMLWriter;
$xml->openURI('php://output');
$xml->setIndent(true); // makes output cleaner
$xml->startElement('qualité');
while ($line = fgetcsv($fp)) {
$xml->startElement('qualité');
$xml->writeElement('critere', $line[0]);
$xml->writeElement('poids', $line[1]);
$xml->writeElement('Vm', $line[2]);
$xml->writeElement('SV', $line[3]);
$xml->writeElement('critere', $line[4]);
$xml->writeElement('poids', $line[5]);
$xml->writeElement('Vm', $line[6]);
$xml->writeElement('SV', $line[7]);
$xml->writeElement('resultat', $line[8]);
$xml->endElement();
}
$xml->endElement();
However there is no result, can anybody help me find where I am going wrong?
The problem is with your line:-
This sends the output directly to the client browser, which I think is what you want to do. However, you need to set the correct headers to reflect this.
Add the following line to your code
and you will see the output directly in your browser. It doesn’t really matter where that line goes, but it must be before any content is sent to the browser, it may be best to put it just before
$fp = fopen('optim.txt', 'r');You should also change the name of the outermost element to whatever the plural of ‘qualité’ is so that you don’t have parent and child elements of the same name.
Also, after
$xml->setIndent(true);add this line$xml->startDocument();otherwise your xml will not be valid.After a bit more digging it seems that it is essential to have a default time zone set (I don’t know why), so add this to the start of your script:-
Which means your code should look like this:-