开发者

How do I convert text to XML in C?

开发者 https://www.devze.com 2022-12-24 01:34 出处:网络
How can I make a program that opens a text file, and converts it into a XML file? The XML might look like:

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>apereira@gmail.com</email>
   <estado>Aprovado</estado>
   <media_notas>13</media_notas>
   <maio开发者_如何学Pythonr_nota>16</maior_nota>
   <menor_nota>11</menor_nota>
  </aluno>
 </lista_alunos>
</curso>


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消