1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>H.�Code for Encoding Conversion Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.61.2"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="apg.html" title="G.�Code for Retrieving Attribute Value Example"><link rel="next" href="api.html" title="I.�Acknowledgements"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">H.�Code for Encoding Conversion Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="apg.html">Prev</a>�</td><th width="60%" align="center">�</th><td width="20%" align="right">�<a accesskey="n" href="api.html">Next</a></td></tr></table><hr></div><div class="appendix" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="convertappendix"></a>H.�Code for Encoding Conversion Example</h2></div></div><div></div></div><p> 2 </p><pre class="programlisting"> 3#include <string.h> 4#include <libxml/parser.h> 5 6 7unsigned char* 8convert (unsigned char *in, char *encoding) 9{ 10 unsigned char *out; 11 int ret,size,out_size,temp; 12 xmlCharEncodingHandlerPtr handler; 13 14 size = (int)strlen(in)+1; 15 out_size = size*2-1; 16 out = malloc((size_t)out_size); 17 18 if (out) { 19 handler = xmlFindCharEncodingHandler(encoding); 20 21 if (!handler) { 22 free(out); 23 out = NULL; 24 } 25 } 26 if (out) { 27 temp=size-1; 28 ret = handler->input(out, &out_size, in, &temp); 29 if (ret || temp-size+1) { 30 if (ret) { 31 printf("conversion wasn't successful.\n"); 32 } else { 33 printf("conversion wasn't successful. converted: %i octets.\n",temp); 34 } 35 free(out); 36 out = NULL; 37 } else { 38 out = realloc(out,out_size+1); 39 out[out_size]=0; /*null terminating out*/ 40 41 } 42 } else { 43 printf("no mem\n"); 44 } 45 return (out); 46} 47 48 49int 50main(int argc, char **argv) { 51 52 unsigned char *content, *out; 53 xmlDocPtr doc; 54 xmlNodePtr rootnode; 55 char *encoding = "ISO-8859-1"; 56 57 58 if (argc <= 1) { 59 printf("Usage: %s content\n", argv[0]); 60 return(0); 61 } 62 63 content = argv[1]; 64 65 out = convert(content, encoding); 66 67 doc = xmlNewDoc ("1.0"); 68 rootnode = xmlNewDocNode(doc, NULL, (const xmlChar*)"root", out); 69 xmlDocSetRootElement(doc, rootnode); 70 71 xmlSaveFormatFileEnc("-", doc, encoding, 1); 72 return (1); 73} 74 75</pre><p> 76 </p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="apg.html">Prev</a>�</td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right">�<a accesskey="n" href="api.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">G.�Code for Retrieving Attribute Value Example�</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">�I.�Acknowledgements</td></tr></table></div></body></html> 77