How do I convert txt file to xml?

user3368897

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
t.h3ads

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I convert an XML file for use in a Rails application?

From Dev

How to convert .txt file into xml file using python?

From Dev

Parsing .txt file to convert it to XML

From Dev

How do I copy the string in a txt file?

From Dev

How do I mark a .txt file as "not executable"?

From Dev

How do I copy the string in a txt file?

From Dev

How do I convert a string array(having xml content) into an XML file?

From Dev

How can I convert a CSV file to XML?

From Dev

How can I convert a CSV file to XML?

From Dev

How do I Convert jSON to XML

From Dev

How do i write the integers that in this file to the mynumbers.txt file?

From Dev

How do I turn a list in a .txt file into a list in a python file?

From Dev

How do I convert csv file to rdd

From Dev

How do I convert an .img file to vhd?

From Dev

How do I convert this to read a zip file?

From Dev

Convert idiosyncratically formatted .txt file to XML

From Dev

How do I find ids in an xml file?

From Dev

How do I mock a XML file in RSpec

From Dev

How to convert this file to xml

From Dev

How to parse a .txt file into .xml?

From Dev

How do I load specific rows from a .txt file in Python?

From Dev

How Do I Open A txt File Without It Erasing The Previous Content

From Dev

How do I save the text of puts in Ruby to a txt file?

From Dev

How do I append to each line of a .txt file using cmd

From Dev

Python: How do I clear a .txt file with the open() function?

From Dev

How do I create a .txt file from a pandas dataframe?

From Dev

How Do I Open A txt File Without It Erasing The Previous Content

From Dev

How do I read a populated array from a .txt file?

From Dev

How do I write the output of Portage to a txt file?

Related Related

HotTag

Archive