(root)/
libxml2-2.12.3/
doc/
tutorial/
includeconvert.c
       1  <![CDATA[
       2  #include <string.h>
       3  #include <libxml/parser.h>
       4  
       5  
       6  unsigned char*
       7  convert (unsigned char *in, char *encoding)
       8  {
       9  	unsigned char *out;
      10          int ret,size,out_size,temp;
      11          xmlCharEncodingHandlerPtr handler;
      12  
      13          size = (int)strlen(in)+1; 
      14          out_size = size*2-1; 
      15          out = malloc((size_t)out_size); 
      16  
      17          if (out) {
      18                  handler = xmlFindCharEncodingHandler(encoding);
      19                  
      20                  if (!handler) {
      21                          free(out);
      22                          out = NULL;
      23                  }
      24          }
      25          if (out) {
      26                  temp=size-1;
      27                  ret = handler->input(out, &out_size, in, &temp);
      28                  if (ret || temp-size+1) {
      29                          if (ret) {
      30                                  printf("conversion wasn't successful.\n");
      31                          } else {
      32                                  printf("conversion wasn't successful. converted: %i octets.\n",temp);
      33                          }
      34                          free(out);
      35                          out = NULL;
      36                  } else {
      37                          out = realloc(out,out_size+1); 
      38                          out[out_size]=0; /*null terminating out*/
      39                          
      40                  }
      41          } else {
      42                  printf("no mem\n");
      43          }
      44          return (out);
      45  }	
      46  
      47  
      48  int
      49  main(int argc, char **argv) {
      50  
      51  	unsigned char *content, *out;
      52  	xmlDocPtr doc;
      53  	xmlNodePtr rootnode;
      54  	char *encoding = "ISO-8859-1";
      55  	
      56  		
      57  	if (argc <= 1) {
      58  		printf("Usage: %s content\n", argv[0]);
      59  		return(0);
      60  	}
      61  
      62  	content = argv[1];
      63  
      64  	out = convert(content, encoding);
      65  
      66  	doc = xmlNewDoc ("1.0");
      67  	rootnode = xmlNewDocNode(doc, NULL, (const xmlChar*)"root", out);
      68  	xmlDocSetRootElement(doc, rootnode);
      69  
      70  	xmlSaveFormatFileEnc("-", doc, encoding, 1);
      71  	return (1);
      72  }
      73  ]]>