Re: [xml] Making Sure Output is XML safe



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]