[xml] How to create an ISO-8859-1 encoded xml file? (following)



Hello,


I apologies for sending this mail to the mailing-list without having 
subscribed, but as the library actually fulfils 100% of my needs, reading the 
archives is enough goood for me,I send this mail just for information.


I tried to create and save an ISO-8859-1 encoded xml file with the following 
code:
(I found the original version in the XML mail archives (11 Apr 2001))
void DoIt(void)
{
  xmlDocPtr doc;
  xmlNodePtr n;
  char l_bufin[1024]="abécè";
  char l_bufout[1024];
  int l_in;
  int l_out;

  doc = xmlNewDoc((unsigned char *)"1.0");
  n = xmlNewNode(NULL, (unsigned char *)"info");
  l_in = strlen(l_bufin);
  l_out = 1000;
  isolat1ToUTF8((unsigned char *)l_bufout, &l_out, (unsigned char *)l_bufin, 
&l_in);
  l_bufout[l_out]=0;
  xmlNodeSetContent(n, (unsigned char *)l_bufout);
++  xmlSetProp(n, (const unsigned char *)"Type", (const unsigned char 
*)l_bufout);

  xmlDocSetRootElement(doc, n);
  xmlSaveFileEnc("test.xml",doc,"ISO-8859-1");
  xmlFreeDoc(doc);
}

The result was:
<?xml version="1.0" encoding="ISO-8859-1"?>
<info Type="ab&#xE9;c&#xE8;">abécè</info>

So I modified the function 'xmlAttrDumpOutput' in tree.c, to get:
<?xml version="1.0" encoding="ISO-8859-1"?>
<info Type="abécè">abécè</info>

If you find it useful, the modified function is the following:
tree.c: 5812

/**
 * xmlAttrDumpOutput:
 * @buf:  the XML buffer output
 * @doc:  the document
 * @cur:  the attribute pointer
 * @encoding:  an optional encoding string
 *
 * Dump an XML attribute
 */
static void
xmlAttrDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, xmlAttrPtr cur,
                  const char *encoding ATTRIBUTE_UNUSED) {
    xmlChar *value;
++  xmlChar *buffer;

    if (cur == NULL) {
#ifdef DEBUG_TREE
        xmlGenericError(xmlGenericErrorContext,
                "xmlAttrDump : property == NULL\n");
#endif
        return;
    }
    xmlOutputBufferWriteString(buf, " ");
    if ((cur->ns != NULL) && (cur->ns->prefix != NULL)) {
        xmlOutputBufferWriteString(buf, (const char *)cur->ns->prefix);
        xmlOutputBufferWriteString(buf, ":");
    }
    xmlOutputBufferWriteString(buf, (const char *)cur->name);

--  value = xmlNodeListGetString(doc, cur->children, 0);
++  value = cur->children->content;
    if (value) {
        xmlOutputBufferWriteString(buf, "=");

++      if (encoding == NULL)
++              buffer = xmlEncodeEntitiesReentrant(doc, value);
++      else
++              buffer = xmlEncodeSpecialChars(doc, value);
++      if (buffer != NULL) {
--              xmlBufferWriteQuotedString(buf->buffer, value);
++              xmlBufferWriteQuotedString(buf->buffer, buffer);
++              xmlFree(buffer);
++      }
--      xmlFree(value);
    } else  {
        xmlOutputBufferWriteString(buf, "=\"\"");
    }
}




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