What I am trying to do is convert my .txt file to xml. I get everything together inside of one tag rather that it being each one in their own tags.
$fp = fopen('notes.txt', 'r');
$xml = new XMLWriter;
$xml->openURI('notes.xml');
$xml->setIndent(true);
$xml->startElement('Notes');
while ($line = fgetcsv($fp)) {
$xml->startElement('note');
$xml->writeElement('dateTime', $line[0]);
$xml->writeElement('Operation', $line[1]);
$xml->writeElement('table', $line[2]);
$xml->writeElement('user', $line[3]);
$xml->endElement();
}
$xml->endElement();
This is what I get:
<Notes>
<note>
<dateTime>2014-03-26 02:43:32 Insert Products Admin</dateTime>
<Operation/>
<table/>
<user/>
</note>
<note>
<dateTime>2014-03-26 02:53:04 Insert Products Admin</dateTime>
<Operation/>
<table/>
<user/>
</note>
<note>
<dateTime>2014-03-26 02:58:13 Insert Products Admin</dateTime>
<Operation/>
<table/>
<user/>
</note>
<note>
</Notes>
Time is in it's correct tag, but "insert" is supposed to be in operation, "products" in table and "admin" in user. How can I fix this so that it places it in correctly?
This is my .txt file:
2014-03-26 02:43:32 Insert Products Admin
2014-03-26 02:53:04 Insert Products Admin
2014-03-26 02:58:13 Insert Products Admin
You should either change the txt file to use a "," as delimiter or configure one for fgetcsv():
$fp = fopen('notes.txt', 'r');
$xml = new XMLWriter;
$xml->openURI('notes.xml');
$xml->setIndent(true);
$xml->startElement('Notes');
while ($line = fgetcsv($fp, 0, "\t")) {
$xml->startElement('note');
$xml->writeElement('dateTime', $line[0]);
$xml->writeElement('Operation', $line[1]);
$xml->writeElement('table', $line[2]);
$xml->writeElement('user', $line[3]);
$xml->endElement();
}
$xml->endElement();
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments