Re: [xml] Making Sure Output is XML safe



Hi Elvis

I seem to have run into another problem :(.
I have a mysql database, storing messages in UTF-8, when I select from it using c/c++ and libmysql.

I get a warning from libxml saying:
    error :
xmlEncodeEntitiesReentrant : input not UTF-8

Im not sure if it is the data that is getting returned by mysql, or the format of the string.
When I store the data plain(latin1)  is seems to work. Only problem is I need to store the
information in UTF-8.

Have you come across a similar problem?

*********************************************************
Danie van der Walt
FoneWorx 
Senior Programmer
Tel : +27112930000
MSN : predetor_me hotmail com
GoogleTalk : predetorlinux gmail com
*********************************************************


Elvis Stansvik wrote:
I forgot the footnote:

[1] http://xmlsoft.org/html/libxml-entities.html#xmlEncodeEntitiesReentrant

Elvis

2008/11/14 Elvis Stansvik <elvstone gmail com>:
  
Hi Danie,

2008/11/5 Danie van der Walt <dvdwalt foneworx co za>:
    
HI Guys

I hope you can help me.
I'm currently using libxml to parse incomming xml, but simply using printf
to generate my reply xml.

I have one variable that may contain characters that are not xml
safe/friendly like '<' as an example.
Is there anyway that I can parse some text to a function and get a xml
"safe/friendly" output that I can use
in my app.
      
Use xmlEncodeEntitiesReentrant() [1] to encode entities in a string. Like this:

#include <stdio.h>

#include <libxml/parser.h>
#include <libxml/entities.h>

int main (int argc, char *argv[])
{
   LIBXML_TEST_VERSION

   const xmlChar *str = "string with < and > in it";
   const xmlChar *xml = "<foo />";

   xmlDoc *doc = xmlReadMemory(xml, 8, "xml", "UTF-8", 0);
   xmlChar *safe_str = xmlEncodeEntitiesReentrant(doc, str);

   printf("%s\n", safe_str);

   xmlFree(safe_str);
   xmlFreeDoc(doc);
   xmlCleanupParser();

   return(0);
}

Note that you need to pass it your document pointer as argument too,
so that it will know about all entities and not just &lt;, &gt; et.c.

Regards,
Elvis

    
Regards
Danie


_______________________________________________
xml mailing list, project page  http://xmlsoft.org/
xml gnome org
http://mail.gnome.org/mailman/listinfo/xml


      

  


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