Re: [xml] encoding question



Hi Marcelo:

   - My first tree is latin1 (ISO-8859-1)
- To create my second tree, I need to create an empty xmlDoc and so, after this, add content. Therefore, my second tree is UTF-8.

        You can do something like the code below...

a.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<a>
  <b>ôxi! vôti!</b>
  <c/>
</a>

lat1-utf8.c:

#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/encoding.h>

int
main (int argc, char **argv)
{
  xmlDocPtr doca, docb;
  xmlNodePtr node, root_node, node_copy;
  unsigned char *text_latin1 = "êita!";
  unsigned char text_utf8[10];
  int len1, len2 = 10;

  len1 = strlen (text_latin1);
  doca = xmlParseFile("a.xml");
  docb = xmlNewDoc("1.0");

  if (!doca || !docb) {
    fprintf(stderr, "Error while parsing a doc\n");
    return 1;
  }

  isolat1ToUTF8(text_utf8, &len2, text_latin1, &len1);
  root_node = xmlNewNode(NULL, "d");
  xmlNewTextChild (root_node, NULL, "e", text_utf8);
  xmlDocSetRootElement(docb, root_node);

  node = xmlDocGetRootElement (doca);
  node = node->xmlChildrenNode;

  while ((xmlStrcmp (node->name, BAD_CAST"c")))
    node = node->next;

  node_copy = xmlCopyNodeList (root_node);

  xmlAddChild (node, node_copy);

  xmlSaveFormatFileEnc("-", doca, "ISO-8859-1",1);

  xmlFree(doca);
  xmlFree(docb);

}

--

[]'s
Lucas Brasilino
brasilino recife pe gov br
http://www.recife.pe.gov.br
Emprel -        Empresa Municipal de Informatica (pt_BR)
                Municipal Computing Enterprise (en_US)
Recife - Pernambuco - Brasil
Fone: +55-81-34167078




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]