[gxml] examples/c: add C examples
- From: Richard Hans Schwarting <rschwart src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gxml] examples/c: add C examples
- Date: Tue, 24 Sep 2013 10:34:08 +0000 (UTC)
commit 5d24649bdb9c64dae152d6207e1aa3e6a0d56ed5
Author: Richard Schwarting <aquarichy gmail com>
Date: Tue Sep 24 06:24:52 2013 -0400
examples/c: add C examples
examples/c/Makefile.am | 2 +-
examples/c/Makefile.example | 2 +-
examples/c/attr.c | 1 +
examples/c/cdata_section.c | 3 ++
examples/c/character_data.c | 74 +++++++++++++++++++++++++++++++++++++++++
examples/c/comment.c | 76 +++++++++++++++++++++++++++++++++++++++++++
examples/c/element.c | 74 +++++++++++++++++++++++++++++++++++++++++
examples/c/node_list.c | 68 ++++++++++++++++++++++++++++++++++++++
examples/c/text.c | 52 +++++++++++++++++++++++++++++
9 files changed, 350 insertions(+), 2 deletions(-)
---
diff --git a/examples/c/Makefile.am b/examples/c/Makefile.am
index e1e66af..62de067 100644
--- a/examples/c/Makefile.am
+++ b/examples/c/Makefile.am
@@ -1,2 +1,2 @@
-EXTRA_DIST = bookshelf.xml Makefile.example output document_create.c document_new.c document_new_from_file.c
document_new_from_path.c document_new_from_string.c document_new_minimal.c document_properties.c
document_save_to_path.c document_save_to_stream.c node.c
+EXTRA_DIST = bookshelf.xml Makefile.example output document_create.c document_new.c document_new_from_file.c
document_new_from_path.c document_new_from_string.c document_new_minimal.c document_properties.c
document_save_to_path.c document_save_to_stream.c node.c comment.c character_data.c text.c node_list.c
element.c
diff --git a/examples/c/Makefile.example b/examples/c/Makefile.example
index d94f265..931f62a 100644
--- a/examples/c/Makefile.example
+++ b/examples/c/Makefile.example
@@ -1,7 +1,7 @@
CC=gcc
CFLAGS=`pkg-config --cflags gxml gio-2.0` -g -Wall
LDFLAGS=`pkg-config --libs gxml gio-2.0`
-PROGS=document_new document_new_minimal document_new_from_string document_new_from_file
document_new_from_path document_save_to_path document_save_to_stream document_properties document_create node
+PROGS=document_new document_new_minimal document_new_from_string document_new_from_file
document_new_from_path document_save_to_path document_save_to_stream document_properties document_create node
comment character_data text node_list element
all: $(PROGS)
diff --git a/examples/c/attr.c b/examples/c/attr.c
new file mode 100644
index 0000000..2499028
--- /dev/null
+++ b/examples/c/attr.c
@@ -0,0 +1 @@
+/* To see how Attr nodes are used, check the Element example */
diff --git a/examples/c/cdata_section.c b/examples/c/cdata_section.c
new file mode 100644
index 0000000..d166739
--- /dev/null
+++ b/examples/c/cdata_section.c
@@ -0,0 +1,3 @@
+/* CDATASections are a type of Text node, which are CharacterData
+ nodes. To see more of what you can do with a CDATASection, view
+ those examples */
diff --git a/examples/c/character_data.c b/examples/c/character_data.c
new file mode 100644
index 0000000..035bb95
--- /dev/null
+++ b/examples/c/character_data.c
@@ -0,0 +1,74 @@
+#include <gxml/gxml.h>
+
+int main () {
+ /* Setup */
+ printf ("CharacterData isn't directly instantiable, but its subclasses Text and Comment are! Go see them
for more additional examples.\n");
+
+ GXmlDocument *doc;
+ GXmlNodeList *books_list;
+ GXmlNode *book;
+
+ doc = gxml_document_new_from_path ("bookshelf_node.xml");
+ books_list = gxml_element_get_elements_by_tag_name (gxml_document_get_document_element (doc), "Book");
+ book = gxml_node_list_nth (books_list, 0);
+
+ GXmlCharacterData *text;
+ GXmlCharacterData *comment;
+ text = GXML_CHARACTER_DATA (gxml_document_create_text_node (doc, "Some infinities are bigger than other
infinities"));
+ comment = GXML_CHARACTER_DATA (gxml_document_create_comment (doc, "As he read, I fell in love the way you
fall asleep:"));
+
+ gchar *str;
+ gchar *str2;
+
+ /* Data representation */
+
+ printf ("Stringifying Text:\n%s\n\n"
+ "Text's CharacterData's data:\n%s\n\n"
+ "Text's CharacterData's length:\n%lu\n\n"
+ "Stringifying Comment:\n%s\n\n"
+ "Comment's CharacterData's data:\n%s\n\n"
+ "Comment's CharacterData's length:\n%lu\n\n",
+ str = gxml_node_to_string (GXML_NODE (text), TRUE, 0),
+ gxml_character_data_get_data (text),
+ gxml_character_data_get_length (text),
+ str2 = gxml_node_to_string (GXML_NODE (comment), TRUE, 0),
+ gxml_character_data_get_data (comment),
+ gxml_character_data_get_length (comment));
+ g_free (str);
+ g_free (str2);
+
+ /* CharacterData operations */
+
+ gxml_node_append_child (book, GXML_NODE (text));
+ printf ("Book with our Text as its child (its content):\n%s\n\n",
+ str = gxml_node_to_string (book, TRUE, 0));
+ g_free (str);
+
+ gxml_node_replace_child (book, GXML_NODE (comment), GXML_NODE (text));
+ printf ("Book with our Comment as its child (its content):\n%s\n\n",
+ str = gxml_node_to_string (book, TRUE, 0));
+ g_free (str);
+
+ gxml_character_data_append_data (comment, " slowly, and then all at once");
+ printf ("Book after appending more data to Comment:\n%s\n\n",
+ str = gxml_node_to_string (book, TRUE, 0));
+ g_free (str);
+
+ gxml_character_data_replace_data (comment, 3, 2, "Gus");
+ printf ("Book after replacing 'he' with 'Gus':\n%s\n\n",
+ str = gxml_node_to_string (book, TRUE, 0));
+ g_free (str);
+
+ gxml_character_data_delete_data (comment, 20, 8);
+ printf ("Book after deleting 'in love ':\n%s\n\n",
+ str = gxml_node_to_string (book, TRUE, 0));
+ g_free (str);
+
+ gxml_character_data_insert_data (comment, 20, "in love ");
+ printf ("Book after inserting 'in love ':\n%s\n",
+ str = gxml_node_to_string (book, TRUE, 0));
+ g_free (str);
+
+ g_object_unref (doc);
+ return 0;
+}
diff --git a/examples/c/comment.c b/examples/c/comment.c
new file mode 100644
index 0000000..41cc032
--- /dev/null
+++ b/examples/c/comment.c
@@ -0,0 +1,76 @@
+#include <gxml/gxml.h>
+
+int main () {
+ /* Setup */
+ GXmlDocument *doc;
+ GXmlNode *bookshelf;
+ gchar *str;
+ GXmlComment *comment;
+
+ doc = gxml_document_new_from_path ("bookshelf_node.xml");
+ bookshelf = GXML_NODE (gxml_document_get_document_element (doc));
+
+ printf ("Bookshelf, without any comments:\n%s\n",
+ str = gxml_node_to_string (bookshelf, TRUE, 0));
+ g_free (str);
+
+ /* Placing comments in the tree */
+
+ comment = gxml_document_create_comment (doc, "this bookshelf is small");
+ gxml_node_insert_before (GXML_NODE (doc), GXML_NODE (comment), bookshelf);
+ printf ("\nAfter trying to insert a comment before our root element:\n%s\n",
+ str = gxml_node_to_string (GXML_NODE (doc), TRUE, 0));
+ g_free (str);
+
+ GXmlNode *owner;
+ comment = gxml_document_create_comment (doc, "its owner is an important author");
+ owner = gxml_node_get_first_child (bookshelf);
+ gxml_node_insert_before (bookshelf, GXML_NODE (comment), owner);
+ printf ("\nAfter inserting a comment before our <owner>:\n%s\n",
+ str = gxml_node_to_string (bookshelf, TRUE, 0));
+ g_free (str);
+
+ GXmlNode *books;
+ comment = gxml_document_create_comment (doc, "I should buy more books");
+ books = gxml_node_get_next_sibling (owner);
+ gxml_node_append_child (books, GXML_NODE (comment));
+ printf ("\nAfter appending a comment in <books>:\n%s\n",
+ str = gxml_node_to_string (bookshelf, TRUE, 0));
+ g_free (str);
+
+ GXmlNode *book;
+ book = gxml_node_get_first_child (books);
+ comment = gxml_document_create_comment (doc, "this pretty cool book will soon be a pretty cool movie");
+ gxml_node_append_child (book, GXML_NODE (comment));
+ printf ("\nAfter adding a comment to an empty <book> node:\n%s\n",
+ str = gxml_node_to_string (bookshelf, TRUE, 0));
+ g_free (str);
+
+ /* Comments as CharacterData */
+ printf ("\nStringified Comments have <!-- -->, like this one:\n%s\n",
+ str = gxml_node_to_string (GXML_NODE (comment), TRUE, 0));
+ g_free (str);
+
+ GXmlCharacterData *chardata = GXML_CHARACTER_DATA (comment);
+
+ printf ("\nComments are CharacterData, so previous comment's data:\n%s\n",
+ gxml_character_data_get_data (chardata));
+
+ printf ("\nComments are CharacterData, so previous comment's length:\n%lu\n",
+ gxml_character_data_get_length (chardata));
+
+ gxml_character_data_append_data (chardata, ". Did you read it?");
+ printf ("\nComment after using CharacterData's append_data ():\n %s\n",
+ str = gxml_node_to_string (GXML_NODE (comment), TRUE, 0));
+ g_free (str);
+
+ gxml_character_data_replace_data (chardata, 12, 4, "awesome");
+ printf ("\nComment after using CharacterData's replace_data () (cool -> awesome).\n"
+ "Note that this affects the data as seen in CharacterData's .data property, excluding the comment's
surrounding <!-- and -->:\n %s\n",
+ str = gxml_node_to_string (GXML_NODE (comment), TRUE, 0));
+ g_free (str);
+
+ g_object_unref (doc);
+
+ return 0;
+}
diff --git a/examples/c/element.c b/examples/c/element.c
new file mode 100644
index 0000000..d6a4aa1
--- /dev/null
+++ b/examples/c/element.c
@@ -0,0 +1,74 @@
+#include <gxml/gxml.h>
+
+int main () {
+ GXmlDocument *doc;
+ GXmlElement *tree;
+
+/* Elements are a type of Node. To see more of what you can do with an Element,
+ view the node examples */
+
+// Setup 1
+ doc = gxml_document_new_from_string ("<Tree type='leafy' wisdom='great'>Yggdrasil</Tree>");
+ tree = gxml_document_get_document_element (doc);
+
+ gchar *str;
+
+ printf ("Stringify Tree element:\n%s\n\n",
+ str = gxml_node_to_string (GXML_NODE (tree), TRUE, 0));
+ g_free (str);
+
+ /* TODO: verify memory handling for these */
+ printf ("Get Tree element's tag name:\n %s\n\n", gxml_element_get_tag_name (tree));
+ printf ("Get Tree element's content:\n %s\n\n", gxml_element_get_content (tree));
+ printf ("Get Tree element's 'type' attribute:\n %s\n\n", gxml_element_get_attribute (tree, "type"));
+
+ gxml_element_set_attribute (tree, "type", "lush");
+ printf ("Change Tree element's 'type' to something else:\n %s\n\n",
+ str = gxml_node_to_string (GXML_NODE (tree), TRUE, 0));
+ g_free (str);
+
+ gxml_element_set_attribute (tree, "roots", "abundant");
+ printf ("Set a new attribute, 'roots':\n %s\n\n",
+ str = gxml_node_to_string (GXML_NODE (tree), TRUE, 0));
+ g_free (str);
+
+ gxml_element_remove_attribute (tree, "type");
+ printf ("Remove attribute 'type':\n %s\n\n",
+ str = gxml_node_to_string (GXML_NODE (tree), TRUE, 0));
+ g_free (str);
+
+ GXmlAttr *new_attr = gxml_document_create_attribute (doc, "height");
+ gxml_attr_set_value (new_attr, "109m");
+ gxml_element_set_attribute_node (tree, new_attr);
+ printf ("Set a new attribute node:\n %s\n\n",
+ str = gxml_node_to_string (GXML_NODE (tree), TRUE, 0));
+ g_free (str);
+
+ GXmlAttr *old_attr = gxml_element_get_attribute_node (tree, "wisdom");
+ printf ("Get an existing attr as a node:\n %s\n\n",
+ str = gxml_node_to_string (GXML_NODE (old_attr), TRUE, 0));
+ g_free (str);
+
+ gxml_element_remove_attribute_node (tree, old_attr);
+ printf ("Remove wisdom attr by its node:\n %s\n\n",
+ str = gxml_node_to_string (GXML_NODE (tree), TRUE, 0));
+ g_free (str);
+
+// Setup 2
+ GXmlDocument *leaves_doc = gxml_document_new_from_string ("<Leaves><Leaf name='Aldo' /><Leaf name='Drona'
/><Leaf name='Elma' /><Leaf name='Hollo' /><Leaf name='Irch' /><Leaf name='Linder' /><Leaf name='Makar'
/><Leaf name='Oakin' /><Leaf name='Olivio' /><Leaf name='Rown' /></Leaves>");
+ GXmlElement *leaves = gxml_document_get_document_element (leaves_doc);
+
+ printf ("Our second example:\n%s\n\n",
+ str = gxml_node_to_string (GXML_NODE (leaves_doc), TRUE, 0));
+ g_free (str);
+ GXmlNodeList *leaf_list;
+ leaf_list = gxml_element_get_elements_by_tag_name (leaves, "Leaf");
+ printf ("Show element descendants of Leaves with the tag name Leaf:\n %s\n",
+ str = gxml_node_list_to_string (leaf_list, TRUE));
+ g_free (str);
+
+/* TODO: when we support sibling text nodes (that is, when we're not
+ * using libxml2), then we can add a normalize () example */
+
+ return 0;
+}
diff --git a/examples/c/node_list.c b/examples/c/node_list.c
new file mode 100644
index 0000000..3ba7b21
--- /dev/null
+++ b/examples/c/node_list.c
@@ -0,0 +1,68 @@
+#include <gxml/gxml.h>
+
+#define GET_NAME_ATTR(x) gxml_element_get_attribute (GXML_ELEMENT (x), "name")
+
+int main () {
+ GXmlDocument *doc;
+
+ doc = gxml_document_new_from_string ("<Refrigerator><Food name='cheese' /><Food name='tomatoes' /><Food
name='avocado' /></Refrigerator>");
+
+ GXmlNodeList *foods_list = gxml_document_get_elements_by_tag_name (doc, "Food");
+
+ gchar *str;
+
+ printf ("Our refrigerator:\n%s\n\n", str = gxml_node_to_string (GXML_NODE (doc), TRUE, 0));
+ g_free (str);
+
+ printf ("The length of the list:\n %lu\n\n", gxml_node_list_get_length (foods_list));
+
+ printf ("Stringify the list:\n %s\n\n", str = gxml_node_list_to_string (foods_list, TRUE));
+ g_free (str);
+
+ printf ("Accessing the second food item from a nodelist using .item ():\n %s\n\n",
+ GET_NAME_ATTR (gxml_node_list_item (foods_list, 1)));
+
+ printf ("Accessing the second food item from a nodelist using .nth ():\n %s\n\n",
+ GET_NAME_ATTR (gxml_node_list_nth (foods_list, 1)));
+
+ printf ("Accessing the second food item from a nodelist using .nth_data ():\n %s\n\n",
+ GET_NAME_ATTR (gxml_node_list_nth_data (foods_list, 1)));
+
+ printf ("Accessing the first food item with .first:\n %s\n\n",
+ GET_NAME_ATTR (gxml_node_list_first (foods_list)));
+
+ printf ("Accessing the last food item with .last:\n %s\n\n",
+ GET_NAME_ATTR (gxml_node_list_last (foods_list)));
+
+ GXmlNode *last = gxml_node_list_last (foods_list);
+ GXmlNode *prev;
+ prev = gxml_node_list_nth_prev (foods_list, last, 2);
+ printf ("Accessing the food item 2 previous to the last with .nth_prev ():\n %s\n\n",
+ GET_NAME_ATTR (prev));
+
+ printf ("Finding the index for the last food item with .find ():\n %d\n\n",
+ gxml_node_list_find (foods_list, last));
+
+ printf ("Finding the index for the last food item with .position ():\n %d\n\n",
+ gxml_node_list_position (foods_list, last));
+
+ printf ("Finding the index for the last food item with .index ():\n %d\n\n",
+ gxml_node_list_index (foods_list, last));
+
+ /* TODO: this isn't wonderfully efficient; we want to do a foreach
+ style loop on it, for (var a : list) {..., or have a NodeList thing
+ to get the next one */
+ printf ("Traverse the list:\n");
+ int i;
+ for (i = 0; i < gxml_node_list_get_length (foods_list); i++) {
+ GXmlNode *food = gxml_node_list_nth (foods_list, i);
+ printf (" %d:%s\n", i, str = gxml_node_to_string (food, TRUE, 0));
+ g_free (str);
+ }
+
+ /* TODO: foreach () */
+
+ /* TODO: find_custom () */
+
+ return 0;
+}
diff --git a/examples/c/text.c b/examples/c/text.c
new file mode 100644
index 0000000..62d4b3e
--- /dev/null
+++ b/examples/c/text.c
@@ -0,0 +1,52 @@
+#include <gxml/gxml.h>
+
+int main () {
+ GXmlDocument *doc;
+ GXmlNode *diary;
+
+ /* Setup */
+ doc = gxml_document_new ();
+ diary = GXML_NODE (gxml_document_create_element (doc, "Diary"));
+ gxml_node_append_child (GXML_NODE (doc), diary);
+
+ GXmlText *text;
+ gchar *str;
+ text = gxml_document_create_text_node (doc, "I am the very model of a modern Major General.");
+
+ printf ("Document before inserting text:\n%s\n\n",
+ str = gxml_node_to_string (GXML_NODE (doc), TRUE, 0));
+ g_free (str);
+
+ gxml_node_append_child (diary, GXML_NODE (text));
+
+ printf ("Document after inserting text:\n%s\n\n",
+ str = gxml_node_to_string (GXML_NODE (doc), TRUE, 0));
+ g_free (str);
+
+ printf ("Nodes of document element 'Diary' before split_text:\n");
+ GXmlNode *node;
+ for (node = gxml_node_get_first_child (diary); node != NULL; node = gxml_node_get_next_sibling (node)) {
+ printf (" Node: %s\n", str = gxml_node_to_string (node, TRUE, 0));
+ g_free (str);
+ }
+
+ gxml_text_split_text (text, 20);
+
+ printf ("\nNodes of document element 'Diary' after split_text:\n");
+ for (node = gxml_node_get_first_child (diary); node != NULL; node = gxml_node_get_next_sibling (node)) {
+ printf (" Node: %s\n", str = gxml_node_to_string (node, TRUE, 0));
+ g_free (str);
+ }
+
+ printf ("\nWARNING: the above does not work as desired, since libxml2 automatically\n"
+ " merges neighbouring Text nodes. You should see two Nodes for each part\n"
+ " of the split. This will hopefully be addressed in the future.\n");
+
+ /* To see Text manipulated as CharacterData, go see the CharacterData example */
+
+ return 0;
+}
+
+
+
+
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]