tags:

views:

63

answers:

2

How can I make a program that opens a text file, and converts it into a XML file?

The XML might look like:

<curso>
 <sigla>LTCGM</sigla>
 <NAlunos>1</NAlunos>
 <lista_alunos>
  <aluno>
   <numero>6567</numero>
   <nome>Artur Pereira Ribeiro</nome>
   <email>[email protected]</email>
   <estado>Aprovado</estado>
   <media_notas>13</media_notas>
   <maior_nota>16</maior_nota>
   <menor_nota>11</menor_nota>
  </aluno>
 </lista_alunos>
</curso>
A: 

There is no simple way to do that - because it is not clear what structure is needed in the markup for the plain text.

What you can do, I suppose, is generate the XML tags on a first line, emit a quasi-arbitrary start tag, copy the file through, expanding XML key characters ('<', '>', '&') into the corresponding '<', '>', '&' notation, and then ending with the end tag corresponding to the start tag.

<?xml version='1.0'?>
<start>
#include &lt;stdio.h&gt;
int main(void) { print("Hello, XML\n"); return 0; }
</start>

Anything more depends on knowledge of what's in the text file.

Jonathan Leffler
+2  A: 

Since XML is generally structured and a text file generally isn't, your first step is to write a parser that reads the text file and understands the structure of the text file. Since I don't know the format of your text file, I cannot give a more specific answer for that point.

Before you write the output, you must escape those characters that have a special meaning in XML, i.e. <, >, &.

Doing the actual output is done e.g. by fprintf, nothing special about that part.

ammoQ