RE: [xml] Problems with the character & and saving/loading XML files
- From: "Daniel Gehriger" <bulk linkcad com>
- To: "'Philip Van Hoof'" <freax pandora be>
- Cc: <xml gnome org>
- Subject: RE: [xml] Problems with the character & and saving/loading XML files
- Date: Fri, 19 Apr 2002 18:06:31 +0200
Use something like the code below. The function copies the escaped input
string to the output buffer of size "len" (including terminating '\0').
It's up to you to ensure that the output buffer is large enough, or the
data will be truncated.
- Daniel
---------------------------------------------------
#include <string.h>
#ifndef MIN
#define MIN(a, b) ((a)<(b)?(a):(b))
#endif
void escape(const char* in, char* out, size_t len)
{
const char * const chars = "<>&'\"";
const char * const entities[] = { "<", ">", "&", "'",
""" };
const char *mark, *set, *end;
size_t l;
end = out + len - 1;
mark = in;
while (*in && out != end) {
set = strstr(chars, in);
if (set) {
/* copy unescaped chars */
l = MIN(in-mark, end-out);
strncpy(out, mark, l);
out += l;
mark = in + 1;
/* insert escaped char */
l = MIN(strlen(entities[set - chars]), end-out);
strncpy(out, entities[set - chars], l);
out += l;
}
++in;
}
/* copy remaining unescaped chars */
l = MIN(in-mark, end-out);
strncpy(out, mark, l);
out += l;
/* need to append '\0', strncpy doesn't always */
*out = '\0';
}
--------------------------------------------------------------
-----Original Message-----
From: Philip Van Hoof [mailto:freax pandora be]
Sent: Friday, 19 April, 2002 16:45
To: Daniel Gehriger
Cc: 'Philip Van Hoof'; xml gnome org
Subject: Re: [xml] Problems with the character & and
saving/loading XML files
On 2002.04.19 15:20 Daniel Gehriger wrote:
The following two characters must be escaped: '<' and '&'.
You could
also use a CDATA section, like this:
<mydata><![CDATA[ ANY DATA ]]></mydata>
, but then you mustn't use "]]>" inside the CDATA section,
and there's
no way to escape this sequence. You better don't use CDATA for this
reason. Here's a link with more information:
http://www.w3schools.com/xml/xml_cdata.asp
Okay, thanks a lot..
How do I use <![CDATA[ in the libxml ? When I do something
like this then "<" and ">" will get escaped :
gchar *data;
data = g_strdup_printf("<![CDATA[%s]]>", item->data);
xmlNewChild (itemnode,NULL,"data", data);
And if I don't want to use the <![CDATA[ ]]> tag. What
would be the best way to search/replace the "special"
characters in C ? Normal regular expression, sed like
search/replace ? s/&/&/g ?
--
Philip Van Hoof
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]