[xml] Problems with the character & and saving/loading XML files




Hi there..

I want to store a struct as an XML file and after that reload the XML file into such a struct. The data of the struct can contain any
character. Including characters like &, <, > , +, " and '. If I save
my struct and there is for example a character "&" in the data; then
loading the XML file fails.

How can I make it this way that I can save "any" data. So the <data></data> tag can contain any character.. do I need to
search/replace XML characters? Or is there a function in the
lib for this?


This is a part of the files mainwin.c and mainwin.h of the project GCM
(on sourceforge). http://gcm.sourceforge.net
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/gcm/gcm/src/mainwin.c


typedef struct _Selection Selection;
typedef struct _SaveAsData SaveAsData;

struct _Selection {
  gchar *time;
  gchar *type;
  gchar *from;
  gchar *data;
  gint row;
};

struct _SaveAsData {
        GList *items;
        gchar *path;
};


gboolean app_save_items_to_disk(gchar *path, SaveAsData *sa)
{
        gint i=0;
        gchar *c;
        xmlDocPtr rootnode;

        rootnode = xmlNewDoc((xmlChar *) "1.0");
        rootnode->root = xmlNewNode(NULL, (xmlChar *) "gcm");
        xmlNewDtd(rootnode, "gcm", "test", "gcmdoc.dtd");

        while (sa->items) {
                xmlNodePtr itemnode;
                Selection *item;
                                i++; c=g_strdup_printf("%d", i);
                item = (Selection*) sa->items->data;
                itemnode = xmlNewNode(NULL, (xmlChar *) "item");
                        xmlNewProp (itemnode, "id", c);
                        xmlNewChild (itemnode,NULL,"type", item->type);
                        xmlNewChild (itemnode,NULL,"time", item->time);
                        xmlNewChild (itemnode,NULL,"from", item->from);
                        xmlNewChild (itemnode,NULL,"data", item->data);
                xmlAddChild (rootnode->root, itemnode);
                sa->items = g_list_next(sa->items);
        }
        g_free(c);
        xmlSaveFile(path, rootnode);
}


/* Loads items from an XML file */
void app_load_items_from_disk(MainWin *mwin, gchar *path)
{
        xmlDocPtr doc;
        xmlNodePtr cur;

        doc = xmlParseFile(path);
        if (doc == NULL) return;
        cur = xmlDocGetRootElement(doc);
        if (cur == NULL) {
                gnome_error_dialog(_("Empty document!"));
                xmlFreeDoc(doc);
                return;
        }
        if (xmlStrcmp(cur->name, (const xmlChar *) "gcm")) {
                gnome_error_dialog(_("This is not a GCM XML file !"));
                xmlFreeDoc(doc);
                return;
        }
        cur = cur->xmlChildrenNode;

        while (cur)
        {
                Selection *item=NULL;
item = (Selection*) malloc(sizeof(Selection));
                memset(item, 0, sizeof(Selection));
                item = parseSelection(doc, cur);
                app_add_selection(mwin, item);
                cur = cur -> next;
        }
        xmlFreeDoc(doc);

}



ps I am using libxml 1.0 I think .. I just placed libxml in my
PKG_CHECK_MODULES() in configure.in :
        PKG_CHECK_MODULES(GCM_DEPENDENCY, gconf libxml)
        AC_SUBST(GCM_DEPENDENCY_CFLAGS)
        AC_SUBST(GCM_DEPENDENCY_LIBS)

ps. Don't be hard on me :) I am pretty new to libxml. Read :
This is the first time that I use the library.


--
Philip Van Hoof



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