When libxml2 is used to process XML files, UTF-8 encoding is used by default. Therefore, when you modify and save the XML file as gb2312 encoding, you must convert the data to UTF-8 encoding before modifying and saving it!
Iconv is used for conversion. The following is the conversion code.
Char * convertenc (char * encfrom, char * encto, const char * in)
{
Static char bufin [1024], bufout [1024], * sin, * sout;
Int mode, Lenin, lenout, RET, nline;
Iconv_t c_pt;
If (c_pt = iconv_open (encto, encfrom) = (iconv_t)-1)
{
Printf ("iconv_open false: % s ==> % s/n", encfrom, encto );
Return NULL;
}
Iconv (c_pt, null );
Lenin = strlen (in) + 1;
Lenout = 1024;
Sin = (char *) in;
Sout = bufout;
Ret = iconv (c_pt, & sin, (size_t *) & Lenin, & sout, (size_t *) & lenout );
If (ret =-1)
{
Return NULL;
}
Iconv_close (c_pt );
Return bufout;
}
The following are examples
Test. xml
<? XML version = "1.0" encoding = "gb2312"?>
<Parent> test </parent>
Read code
Int main (void)
{
Xmldocptr Doc = NULL;
Xmlnodeptr cur = NULL;
Doc = xmlparsefile ("test. xml ");
Cur = xmldocgetrootelement (DOC );
Printf ("% s/n", convert ("UTF-8", "gb2312", (char *) xmlnodegetcontent (cur )));
}
Modify and save code
Test2.xml
<? XML version = "1.0" encoding = "gb2312"?>
<Story>
<Storyinfo>
<Author> JOHN fleck </author>
<Datewritten> June 2, 2002 </datewritten>
<Keyword> example keyword </keyword>
<Bibliography> C ++ </bibliography>
<Test> test </test>
</Storyinfo>
<Body>
<Headline> This is the headline <Para> This is the body text. </para>
</Body>
</Story>
Xmldocptr
Parsedoc (char * docname, char * URI ){
Xmldocptr Doc;
Xmlnodeptr cur;
Xmlnodeptr newnode;
Xmlattrptr newattr;
Doc = xmlparsefile (docname );
If (Doc = NULL ){
Fprintf (stderr, "document not parsed successfully./N ");
Return (null );
}
Cur = xmldocgetrootelement (DOC );
If (cur = NULL ){
Fprintf (stderr, "empty document/N ");
Xmlfreedoc (DOC );
Return (null );
}
If (xmlstrcmp (cur-> name, (const xmlchar *) "story ")){
Fprintf (stderr, "Document of the wrong type, root node! = Story ");
Xmlfreedoc (DOC );
Return (null );
}
Newnode = xmlnewtextchild (cur, null, (xmlchar *) "Reference", null );
Newattr = xmlnewprop (newnode, (xmlchar *) "Uri", (xmlchar *) URI );
Return (DOC );
}
Int main (INT argc, char ** argv)
{
Int Options = 0;
Xmldocptr Doc = NULL;
Char * output = NULL;
Char * sznode = NULL;
Int ret = 0;
Xmladdencodingalias ("UTF-8", "dvenc ");
Xmlkeepblanksdefault (0 );
Defaultentityloader = xmlgetexternalentityloader ();
Xmlsetexternalentityloader (xmllintexternalentityloader );
Xmllinenumbersdefault (1 );
Sznode = convert ("gb2312", "UTF-8", "test ");
Doc = parsedoc (argv [2], sznode );
Ret = xmlsaveformatfileenc (output? Output: "-", Doc, argv [1], 1 );
If (Ret <0)
{
Fprintf (stderr, "failed save to % s/n ",
Output? Output :"-");
}
Return 0;
}
Run
Xmlout gb2312 test2.xml
Result
<? XML version = "1.0" encoding = "gb2312"?>
<Story>
<Storyinfo>
<Author> JOHN fleck </author>
<Datewritten> June 2, 2002 </datewritten>
<Keyword> example keyword </keyword>
<Bibliography> C ++ </bibliography>
<Test> test </test>
</Storyinfo>
<Body>
<Headline> This is the headline <Para> This is the body text. </para>
</Body>
<Reference uri = "test"/>
</Story>