|
My first XML document |
This example create a new simple document and save it to disk.
#include <stdio.h>
#include <gdome.h>
int main (int argc, char **argv) {
GdomeDOMImplementation *domimpl;
GdomeDocument *doc;
GdomeElement *root, *el;
GdomeText *txtnode;
GdomeException exc;
GdomeNode *result;
GdomeDOMString *name, *value;
/* First I get a DOMImplementation reference */
domimpl = gdome_di_mkref ();
/* I create a new document with TEST as root element */
name = gdome_str_mkref ("TEST");
doc = gdome_di_createDocument(domimpl, NULL, name, NULL, &exc);
if (doc == NULL) {
fprintf (stderr, "DOMImplementation.createDocument: failed\n\tException #%d\n", exc);
return 1;
}
gdome_str_unref (name);
/* I get reference to the root element of the document */
root = gdome_doc_documentElement (doc, &exc);
if (root == NULL) {
fprintf (stderr, "Document.documentElement: NULL\n\tException #%d\n", exc);
return 1;
}
/* I set an attribute to the root element */
name = gdome_str_mkref ("PACKAGE");
value = gdome_str_mkref ("gdome2");
gdome_el_setAttribute (root, name, value, &exc);
if (exc) {
fprintf (stderr, "Element.setAttribute: failed\n\tException #%d\n", exc);
return 1;
}
gdome_str_unref (name);
gdome_str_unref (value);
/* I create a new element called RELEASE */
name = gdome_str_mkref ("RELEASE");
el = gdome_doc_createElement (doc, name, &exc);
if (el == NULL) {
fprintf (stderr, "Document.createElement: NULL\n\tException #%d\n", exc);
return 1;
}
gdome_str_unref (name);
/* I create a Text Node*/
value = gdome_str_mkref ("0.8.1");
txtnode = gdome_doc_createTextNode (doc, value, &exc);
if (txtnode == NULL) {
fprintf (stderr, "Document.createTextNode: NULL\n\tException #%d\n", exc);
return 1;
}
gdome_str_unref (value);
/* I append the Text Node created to the childs list of the RELEASE element */
result = gdome_el_appendChild (el, (GdomeNode *)txtnode, &exc);
if (result != (GdomeNode *)txtnode) {
fprintf (stderr, "Element.appendChild: failed\n\tException #%d\n", exc);
return 1;
}
gdome_t_unref(txtnode, &exc);
gdome_n_unref(result, &exc);
/* I append the RELEASE element to the childs list of the root element */
result = gdome_el_appendChild (root, (GdomeNode *)el, &exc);
if (result != (GdomeNode *)el) {
fprintf (stderr, "Element.appendChild: failed\n\tException #%d\n", exc);
return 1;
}
gdome_el_unref(el, &exc);
gdome_el_unref(root, &exc);
gdome_n_unref(result, &exc);
/* I save the created document to a file named "examplea.xml */
if (!gdome_di_saveDocToFile (domimpl, doc, LOCALDIR"/examplea.xml", GDOME_SAVE_STANDARD, &exc)) {
fprintf (stderr, "DOMImplementation.saveDocToFile: failed\n\tException #%d\n", exc);
return 1;
}
/* I free the document structure and the DOMImplementation */
gdome_doc_unref (doc, &exc);
gdome_di_unref (domimpl, &exc);
return 0;
}
The file saved to disk (examplea.xml) should be like this:
<?xml version="1.0"?>
<TEST PACKAGE="gdome2"><RELEASE>0.8.1</RELEASE></TEST>
|
Top | Home
|
|
|
Load and Save XML document |
This example load a document from an XML file, make some changes to the document and then save it to disk.
The input file (exampleb.xml) is:
<TEST>
<NODE1/>
<NODE2/>
<NODE3/>
<NODE4/>
<NODE5/>
</TEST>
The source file is:
#include <stdio.h>
#include <gdome.h>
int main (int argc, char **argv) {
GdomeDOMImplementation *domimpl;
GdomeDocument *doc;
GdomeElement *root, *el;
GdomeNodeList *childs;
GdomeException exc;
GdomeDOMString *name, *value;
unsigned long i, nchilds;
/* First I get a DOMImplementation reference */
domimpl = gdome_di_mkref ();
/* I load a new document from the file name "exampleb.xml */
doc = gdome_di_createDocFromURI(domimpl, LOCALDIR"/exampleb.xml", GDOME_LOAD_PARSING, &exc);
if (doc == NULL) {
fprintf (stderr, "DOMImplementation.createDocFromURI: failed\n\tException #%d\n", exc);
return 1;
}
/* I get reference to the root element of the document */
root = gdome_doc_documentElement (doc, &exc);
if (root == NULL) {
fprintf (stderr, "Document.documentElement: NULL\n\tException #%d\n", exc);
return 1;
}
/* I get the reference to the childrens NodeList of the root element */
childs = gdome_el_childNodes (root, &exc);
if (childs == NULL) {
fprintf (stderr, "Element.childNodes: NULL\n\tException #%d\n", exc);
return 1;
}
/* I add the attribute CHECKED="yes" to all element childs of the root element */
name = gdome_str_mkref ("CHECKED");
value = gdome_str_mkref ("yes");
nchilds = gdome_nl_length (childs, &exc);
for (i = 0; i < nchilds; i++) {
el = (GdomeElement *)gdome_nl_item (childs, i, &exc);
if (el == NULL) {
fprintf (stderr, "NodeList.item(%d): NULL\n\tException #%d\n", (int)i, exc);
return 1;
}
if (gdome_el_nodeType (el, &exc) == GDOME_ELEMENT_NODE) {
gdome_el_setAttribute (el, name, value, &exc);
if (exc) {
fprintf (stderr, "Element.setAttribute: failed\n\tException #%d\n", exc);
return 1;
}
}
gdome_el_unref (el, &exc);
}
gdome_nl_unref (childs, &exc);
gdome_el_unref (root, &exc);
/* I save the modified document to a file named "exampleb_out.xml */
if (!gdome_di_saveDocToFile (domimpl, doc, LOCALDIR"/exampleb_out.xml", GDOME_SAVE_STANDARD, &exc)) {
fprintf (stderr, "DOMImplementation.saveDocToFile: failed\n\tException #%d\n", exc);
return 1;
}
/* I free the document structure and the DOMImplementation */
gdome_doc_unref (doc, &exc);
gdome_di_unref (domimpl, &exc);
return 0;
}
The file saved to disk (exampleb_out.xml) should be like this:
<TEST>
<NODE1 CHECKED="yes"/>
<NODE2 CHECKED="yes"/>
<NODE3 CHECKED="yes"/>
<NODE4 CHECKED="yes"/>
<NODE5 CHECKED="yes"/>
</TEST>
|
Top | Home
|
|
|
Cleaning traversal |
This example load a document from an XML file, remove all comment and blank nodes from the document and then save it to disk.
The input file (examplec.xml) is:
<?xml version="1.0"?>
<TEST>
<NODE1>
<!-- Comment -->
</NODE1>
<NODE2>
<!-- Comment -->
<NODE3>
<!-- Comment -->
<NODE4>
<!-- Comment -->
</NODE4>
<NODE5>
</NODE5>
<!-- Comment -->
<!-- Comment -->
</NODE3>
</NODE2>
</TEST>
The source file is:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <gdome.h>
int isSpaceStr (GdomeDOMString *domstr) {
gchar *str = domstr->str;
for (; *str; str++)
if (!isspace(*str))
return 0;
return 1;
}
void cleanSubTree (GdomeNode *node) {
GdomeNode *child = NULL, *dchild = NULL;
GdomeNodeList *nl = NULL;
GdomeException exc;
GdomeDOMString *str;
long i, nllength;
nl = gdome_n_childNodes (node, &exc);
if ((nllength = gdome_nl_length (nl, &exc)) == 0) {
if (nl != NULL)
gdome_nl_unref (nl, &exc);
return;
}
for (i = nllength-1; i >= 0; i--) {
child = gdome_nl_item (nl, i, &exc);
str = NULL;
if (gdome_n_nodeType (child, &exc) == GDOME_COMMENT_NODE ||
(gdome_n_nodeType (child, &exc) == GDOME_TEXT_NODE &&
isSpaceStr ((str = gdome_t_data ((GdomeText *)child, &exc))))) {
dchild = gdome_n_removeChild (node, child, &exc);
if (str != NULL)
gdome_str_unref (str);
if (dchild != NULL)
gdome_n_unref (dchild, &exc);
}
else if (gdome_n_hasChildNodes (child, &exc))
cleanSubTree (child);
if (child != NULL)
gdome_n_unref (child, &exc);
}
if (nl != NULL)
gdome_nl_unref (nl, &exc);
}
int main (int argc, char **argv) {
GdomeDOMImplementation *domimpl;
GdomeDocument *doc;
GdomeElement *rootel;
GdomeException exc;
/* First I get a DOMImplementation reference */
domimpl = gdome_di_mkref ();
/* I load a new document from the file name "exampleb.xml */
doc = gdome_di_createDocFromURI(domimpl, LOCALDIR"/examplec.xml", GDOME_LOAD_PARSING, &exc);
if (doc == NULL) {
fprintf (stderr, "DOMImplementation.createDocFromURI: failed\n\tException #%d\n", exc);
return 1;
}
/* I get reference to the root element of the document */
rootel = gdome_doc_documentElement (doc, &exc);
if (rootel == NULL) {
fprintf (stderr, "Document.documentElement: NULL\n\tException #%d\n", exc);
return 1;
}
cleanSubTree ((GdomeNode *)rootel);
gdome_el_unref (rootel, &exc);
if (!gdome_di_saveDocToFile (domimpl, doc, LOCALDIR"/examplec_out.xml", GDOME_SAVE_STANDARD, &exc)) {
fprintf (stderr, "DOMImplementation.saveDocToFile: failed\n\tException #%d\n", exc);
return 1;
}
/* I free the document structure and the DOMImplementation */
gdome_doc_unref (doc, &exc);
gdome_di_unref (domimpl, &exc);
return 0;
}
The file saved to disk (examplec_out.xml) should be like this:
<?xml version="1.0"?>
<TEST><NODE1/><NODE2><NODE3><NODE4/><NODE5/></NODE3></NODE2></TEST>
|
Top | Home
|
|
|
|