[xml] encoding
- From: "Thomas Dold" <thomas dold gmx de>
- To: <xml gnome org>
- Subject: [xml] encoding
- Date: Mon, 2 Apr 2001 21:22:38 +0200
Another two question to the encoding:
First:
In the following program I set the property "town" which the value
"München".
Before I call the xmlSetProp-Function, I transform the value-string with the
function isolat1ToUTF8.
After setting the property, I read it again with the function xmlGetProp.
The returned string is in UTF-8, so I use the function UTF8Toisolat1.
But when I then print the string, I only get "Mnchen".
Then I save the document in a file (xmlSaveFile), and parse it again
(xmlParseFile).
Then I read once more the property on the way described above, but now I
get the strin "München".
And second:
As you can see, I try to save the file in different encodings. Only the
saving
without encoding has no error, and so it's in UTF-8 (or not?).
Why do I get the error, that the document is not in UTF-8, when I try to
save in
in another encoding?
Can someone help me? What's wrong?
Thanks.
Thomas
The Program:
#include <stdio.h>
#include "libxml/parser.h"
#include "libxml/tree.h"
#include "libxml/xmlmemory.h"
#include "libxml/xpath.h"
#include "libxml/encoding.h"
int
main(int argc, char **argv)
{
xmlDocPtr doc;
xmlNodePtr root, node;
int i, rc;
xmlXPathContextPtr ctx;
xmlXPathObjectPtr path;
const xmlChar *prop;
xmlChar stringISO[50];
xmlChar stringUTF[50];
xmlChar* pStringUTF;
xmlChar* pStringISO;
int lenISO;
int lenUTF;
/* Test One */
printf("Test1...\n");
/* build the document */
doc = xmlNewDoc("1.0");
root = xmlNewDocNode(doc, NULL, "root", NULL);
node = xmlDocSetRootElement(doc, root);
node = xmlNewChild(root, NULL,"node", NULL);
/* set a property */
strcpy(stringISO, "München");
lenISO = strlen(stringISO);
lenUTF = sizeof(stringUTF);
memset(stringUTF, 0, lenUTF);
rc = isolat1ToUTF8(stringUTF, &lenUTF, stringISO, &lenISO);
xmlSetProp(node, "town", stringUTF);
/* read the property */
pStringUTF = xmlGetProp(node, (xmlChar*)"town");
lenUTF = strlen(pStringUTF);
lenISO = sizeof(stringISO);
memset(stringISO, 0, lenISO);
rc = UTF8Toisolat1(stringISO, &lenISO, pStringUTF, &lenUTF);
printf("...with property town=%s\n",stringISO);
printf("Save in UTF8: ");
rc = xmlSaveFile("test1.xml", doc);
if (rc > 0) printf(" OK\n"); else printf(" not OK, rc=%i\n",rc);
printf("Save in ISO-Latin-1: ");
rc = xmlSaveFileEnc("test1-ISO.xml", doc, "ISO-Latin-1");
if (rc > 0) printf(" OK\n"); else printf(" not OK, rc=%i\n",rc);
printf("Save in ISO-8859-1: ");
rc = xmlSaveFileEnc("test1-ISO2.xml", doc, "ISO-8859-1");
if (rc > 0) printf(" OK\n"); else printf(" not OK, rc=%i\n",rc);
xmlFreeDoc(doc);
printf("Test1-Ende...\n");
/*-------------------------------------------------------------------*/
printf("Test2...\n");
/* parse the file */
doc = xmlParseFile("test1.xml");
xmlXPathInit();
ctx = xmlXPathNewContext(doc);
path = xmlXPathEvalExpression((const xmlChar *)"/child::*/child::*", ctx);
if (path)
{
if (path->nodesetval->nodeNr > 0)
{
node = path->nodesetval->nodeTab[0];
pStringUTF = xmlGetProp(node, (xmlChar*)"town");
lenUTF = strlen(pStringUTF);
lenISO = sizeof(stringISO);
memset(stringISO, 0, lenISO);
rc = UTF8Toisolat1(stringISO, &lenISO, pStringUTF, &lenUTF);
printf("...with property town=%s\n",stringISO);
}
xmlXPathFreeObject(path);
}
xmlXPathFreeContext(ctx);
printf("Save in ISO-8859-1: ");
rc = xmlSaveFileEnc("test2out.xml", doc, "ISO-8859-1");
if (rc > 0) printf("OK\n");
printf("Test2-Ende...\n");
xmlFreeDoc(doc);
return (0);
}
The Output:
Test1...
...with property town=Mnchen
Save in UTF8: OK
xmlSaveFileEnc: document not in UTF8
Save in ISO-Latin-1: not OK, rc=-1
xmlSaveFileEnc: document not in UTF8
Save in ISO-8859-1: not OK, rc=-1
Test1-Ende...
Test2...
...with property town=München
Save in ISO-8859-1: OK
Test2-Ende...
[Date Prev][
Date Next] [Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]