[gdome] problem with gdome_di_saveDocToFile()



Kindly see the code below.

If I create an element in the main, it's doing fine. The moment I write
my own function to do that, it gives segfault.

Regards,
Sam Oak.
#include <stdio.h>
#include <libgdome/gdome.h>

GdomeElement *create_DOM_element(GdomeDocument **doc, GdomeElement **elParent, char *strName, char *strValue)
{
	GdomeElement *el;
	GdomeNode *result;
	GdomeException exc;
	GdomeDOMString *name, *value;
	GdomeText *txtnode;

	/* Creates new element having name strName */
	name = gdome_str_mkref(strName);
	el = gdome_doc_createElement(*doc, name, &exc);
	if(el == NULL)
	{
		gdome_str_unref(name);
		fprintf(stderr, "<%s> Document.createElement: NULL\nException: #d\n", __func__, exc);
		return (GdomeElement *)NULL;
	}
	gdome_str_unref(name);

	fprintf(stderr, "<%s> Element created.\n", __func__);

	/* Creates a text node for value of element "el" */
	value = gdome_str_mkref(strValue);
	txtnode = gdome_doc_createTextNode(*doc, value, &exc);
	if(NULL == txtnode)
	{
		gdome_str_unref(value);
		fprintf(stderr, "<%s> Document.createTextNode: NULL\nException: #d\n", __func__, exc);
		return (GdomeElement *)NULL;
	}
	gdome_str_unref(value);
	fprintf(stderr, "<%s> Assigned text value to element.\n", __func__);

	/* Now, appends the text node to the child of element elParent */
	result = gdome_el_appendChild(*elParent, (GdomeNode *)txtnode, &exc);
	if(result != (GdomeNode *)txtnode)
	{
		gdome_t_unref(txtnode, &exc);
		fprintf(stderr, "<%s> Element.appendChild: FAILED\nException: #d\n", __func__, exc);
		return (GdomeElement *)NULL;
	}
	gdome_t_unref(txtnode, &exc);
	gdome_n_unref(result, &exc);
	fprintf(stderr, "<%s> Appends text node as a child to element.\n", __func__);

	/* Appends the text node to the child of this element */
	result = gdome_el_appendChild(el, (GdomeNode *)txtnode, &exc);
	if(result != (GdomeNode *)txtnode)
	{
		if(result) gdome_n_unref(result, &exc);
		fprintf(stderr, "<%s> Element.appendChild: FAILED\nException: #d\n", __func__, exc);
		return (GdomeElement *)NULL;
	}
	gdome_t_unref(txtnode, &exc);
	gdome_n_unref(result, &exc);

	fprintf(stderr, "<%s> Appends element as a child to element.\n", __func__);

	return el;
}

int main(int argc, char **argv)
{
	GdomeDOMImplementation *domimpl;
	GdomeDocument *doc;
	GdomeElement *root, *elResult; 
	GdomeText *txtnode;
	GdomeException exc;
	GdomeNode *result;
	GdomeDOMString *name, *value;

	/* gets a DOMImplementation reference */
	domimpl = gdome_di_mkref();

	/* Creates new document named "data" as root element */
	name = gdome_str_mkref("data");
	doc = gdome_di_createDocument(domimpl, NULL, name, NULL, &exc);
	if(doc == NULL)
	{
		fprintf(stderr, "<%s> DOMImplementation.createDocument: FAILED\nException #%d\n", __func__, exc);
		return -1;
	}
	gdome_str_unref(name);

	/* Gets reference to the root element of the document */
	root = gdome_doc_documentElement(doc, &exc);
	if(root == NULL)
	{
		fprintf (stderr, "<%s> Document.documentElement: NULL\nException #%d\n", __func__, exc);
		return -1;
	}

	/* Creates new element with name and value */
	elResult = create_DOM_element(&doc, &root, "type", "1");

	if(NULL == elResult)
	{
		fprintf(stderr, "<%s> create_DOM_element FAILED.\n", __func__);
		return -1;
	}

	gdome_el_unref(root, &exc);

	/* Saves the created document to a file named "test.xml */
	if(!gdome_di_saveDocToFile(domimpl, doc, "test.xml", GDOME_SAVE_STANDARD, &exc))
	{
		fprintf(stderr, "<%s> DOMImplementation.saveDocToFile: failed\nException #%d\n", __func__, exc);
		return -1;
	}

	/* Frees the document structure and the DOMImplementation */
	gdome_doc_unref(doc, &exc);
	gdome_di_unref(domimpl, &exc);

	return 0;
}


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