Skip to content
Snippets Groups Projects
Commit ba58fa23 authored by Richard Schwarting's avatar Richard Schwarting
Browse files

Examples: add examples in C, Vala, JS, and Python on how to create and save documents

parent 79855ff4
Branches
Tags
No related merge requests found
CC=gcc
CFLAGS=`pkg-config --cflags gxml gio-2.0` -g -Wall
LDFLAGS=`pkg-config --libs gxml gio-2.0`
example: example.o
clean:
rm example *.o
<?xml version="1.0"?>
<Bookshelf>
<Owner fullname="John Green"/>
<Books>
<Book author="John Green" title="The Fault in Our Stars"/>
<Book author="Jane Austen" title="Pride &amp; Prejudice"/>
<Book author="J.D. Salinger" title="Nine Stories"/>
</Books>
</Bookshelf>
#include <gxml/gxml.h>
#include <stdio.h>
int handle_error (GError *error) {
// TODO: better way to check whether it was set?
if (error != NULL) {
g_error (error->message);
return 1;
}
return 0;
}
int create_a_document () {
GError *error = NULL;
GXmlDocument *doc;
GXmlElement *root;
GXmlElement *owner;
GXmlElement *books;
GXmlElement *book;
int i;
char *str;
char *authors[] = { "John Green", "Jane Austen", "J.D. Salinger" };
char *titles[] = { "The Fault in Our Stars", "Pride & Prejudice", "Nine Stories" };
doc = gxml_document_new (&error);
handle_error (error);
// Add a root node
root = gxml_document_create_element (doc, "Bookshelf", &error);
handle_error (error);
gxml_dom_node_append_child (GXML_DOM_NODE (doc), GXML_DOM_NODE (root), &error);
handle_error (error);
// if we tried to add a second one, it would fail and GError would be set :)
// Add an owner node
owner = gxml_document_create_element (doc, "Owner", &error);
handle_error (error);
gxml_dom_node_append_child (GXML_DOM_NODE (root), GXML_DOM_NODE (owner), &error);
handle_error (error);
gxml_element_set_attribute (owner, "fullname", "John Green", &error);
// TODO: need to figure out what sort of errors these would return,
// want the devhelp pages to describe meaningful possible errors
handle_error (error);
// Add a collection of books
books = gxml_document_create_element (doc, "Books", &error);
handle_error (error);
gxml_dom_node_append_child (GXML_DOM_NODE (root), GXML_DOM_NODE (books), &error);
for (i = 0; i < sizeof (authors) / sizeof (char*); i++) {
book = gxml_document_create_element (doc, "Book", &error);
handle_error (error);
gxml_element_set_attribute (book, "author", authors[i], &error);
handle_error (error);
gxml_element_set_attribute (book, "title", titles[i], &error);
handle_error (error);
gxml_dom_node_append_child (GXML_DOM_NODE (books), GXML_DOM_NODE (book), &error);
handle_error (error);
}
// TODO: want a "gxml_dom_node_from_string ()"; make sure document ownership transfer properly
str = gxml_dom_node_to_string (GXML_DOM_NODE (doc), TRUE, 2);
printf ("create_a_document:\n%s\n", str);
g_free (str);
g_object_unref (doc);
g_object_unref (root); // TODO: figure out what happens to root if you deallocate doc?! */
// TODO: perhaps see whether we can make it that all the nodes that are returned are weak references
g_object_unref (owner);
g_object_unref (books);
g_object_unref (book);
/* In util/ there's a small programme that tells you when ownership
is transfered (that is, you are given a return value that you own
and must free. */
return 0;
}
int create_a_document_minimal () {
GXmlDocument *doc;
GXmlElement *root;
GXmlElement *owner;
GXmlElement *books;
GXmlElement *book;
int i;
char *str;
char *authors[] = { "John Green", "Jane Austen", "J.D. Salinger" };
char *titles[] = { "The Fault in Our Stars", "Pride & Prejudice", "Nine Stories" };
doc = gxml_document_new (NULL);
// Add a root node
root = gxml_document_create_element (doc, "Bookshelf", NULL);
gxml_dom_node_append_child (GXML_DOM_NODE (doc), GXML_DOM_NODE (root), NULL);
// if we tried to add a second one, it would fail and GError would be set :)
// Add an owner node
owner = gxml_document_create_element (doc, "Owner", NULL);
gxml_dom_node_append_child (GXML_DOM_NODE (root), GXML_DOM_NODE (owner), NULL);
gxml_element_set_attribute (owner, "fullname", "John Green", NULL);
// TODO: need to figure out what sort of errors these would return,
// want the devhelp pages to describe meaningful possible errors
// Add a collection of books
books = gxml_document_create_element (doc, "Books", NULL);
gxml_dom_node_append_child (GXML_DOM_NODE (root), GXML_DOM_NODE (books), NULL);
for (i = 0; i < sizeof (authors) / sizeof (char*); i++) {
book = gxml_document_create_element (doc, "Book", NULL);
gxml_element_set_attribute (book, "author", authors[i], NULL);
gxml_element_set_attribute (book, "title", titles[i], NULL);
gxml_dom_node_append_child (GXML_DOM_NODE (books), GXML_DOM_NODE (book), NULL);
}
str = gxml_dom_node_to_string (GXML_DOM_NODE (doc), TRUE, 2);
printf ("create_a_document_minimal:\n%s\n", str);
g_free (str);
g_object_unref (doc);
g_object_unref (root);
g_object_unref (owner);
g_object_unref (books);
g_object_unref (book);
// TODO: how do we clean them up?
return 0;
}
int create_a_document_from_a_string () {
char *xml;
char *str;
GXmlDocument *doc;
xml = "<?xml version=\"1.0\"?><Bookshelf><Owner fullname=\"John Green\"/><Books><Book author=\"John Green\" title=\"The Fault in Our Stars\"/><Book author=\"Jane Austen\" title=\"Pride &amp; Prejudice\"/><Book author=\"J.D. Salinger\" title=\"Nine Stories\"/></Books></Bookshelf>";
doc = gxml_document_new_from_string (xml, NULL);
str = gxml_dom_node_to_string (GXML_DOM_NODE (doc), TRUE, 2);
printf ("create_a_document_from_a_string:\n%s\n", str);
g_free (str);
g_object_unref (doc);
return 0;
}
int create_a_document_from_a_file () {
GXmlDocument *doc;
GFile *file;
char *str;
GCancellable *can = g_cancellable_new ();
GError *error = NULL;
file = g_file_new_for_path ("bookshelf.xml");
doc = gxml_document_new_from_gfile (file, can, &error); // TODO: want to change this function name to "from file"
handle_error (error);
str = gxml_dom_node_to_string (GXML_DOM_NODE (doc), TRUE, 2);
printf ("create_a_document_from_a_file:\n%s\n", str);
g_free (str);
g_object_unref (doc);
// TODO: how do you free a GFile? g_object_unref ()?
return 0;
}
int create_a_document_from_a_path () {
GXmlDocument *doc;
char *str;
doc = gxml_document_new_from_path ("bookshelf.xml", NULL);
str = gxml_dom_node_to_string (GXML_DOM_NODE (doc), TRUE, 2);
printf ("create_a_document_from_a_file:\n%s\n", str);
g_free (str);
g_object_unref (doc);
return 0;
}
int saving_a_document_to_a_path () {
GXmlDocument *doc;
doc = gxml_document_new_from_path ("bookshelf.xml", NULL);
gxml_document_save_to_path (doc, "bookshelf2.xml");
g_object_unref (doc);
return 0;
}
int saving_a_document_to_a_stream () {
GXmlDocument *doc;
GFile *outfile;
GFileOutputStream *outstream;
GCancellable *can = g_cancellable_new ();
GError *error = NULL;
doc = gxml_document_new_from_path ("bookshelf.xml", &error);
outfile = g_file_new_for_path ("bookshelf3.xml");
outstream = g_file_replace (outfile, NULL, FALSE, G_FILE_CREATE_REPLACE_DESTINATION, can, &error);
gxml_document_save_to_stream (doc, G_OUTPUT_STREAM (outstream), can, &error);
g_object_unref (doc);
// TODO: figure out how to close/unref outstream and outfile
return 0;
}
int main (void) {
g_type_init ();
create_a_document (); // has token error checking
create_a_document_minimal ();
create_a_document_from_a_string ();
create_a_document_from_a_file ();
create_a_document_from_a_path ();
saving_a_document_to_a_path ();
saving_a_document_to_a_stream ();
/* TODO: add these examples */
// find_nodes_of_given_tag ();
// find_nodes_matching_an_attribute ();
// accessing_attributes ();
// find_
return 0;
}
<?xml version="1.0"?>
<Bookshelf>
<Owner fullname="John Green"/>
<Books>
<Book author="John Green" title="The Fault in Our Stars"/>
<Book author="Jane Austen" title="Pride &amp; Prejudice"/>
<Book author="J.D. Salinger" title="Nine Stories"/>
</Books>
</Bookshelf>
#!/usr/bin/gjs
const GXml = imports.gi.GXml;
const Gio = imports.gi.Gio;
function create_a_document () {
var authors = [ "John Green", "Jane Austen", "J.D. Salinger" ];
var titles = [ "The Fault in Our Stars", "Pride & Prejudice", "Nine Stories" ];
try {
let doc = GXml.Document.new ();
let root = doc.create_element ("Bookshelf");
doc.append_child (root);
let owner = doc.create_element ("Owner");
root.append_child (owner);
owner.set_attribute ("fullname", "John Green");
let books = doc.create_element ("Books");
root.append_child (books);
for (var i = 0; i < authors.length; i++) {
let book = doc.create_element ("Book");
book.set_attribute ("author", authors[i]);
book.set_attribute ("title", titles[i]);
books.append_child (book);
}
print ("create_a_document:\n" + doc.to_string (true, 4));
} catch (error) {
print (error.message);
}
}
function create_a_document_from_a_string () {
let xml = "<?xml version=\"1.0\"?>\
<Bookshelf>\
<Owner fullname=\"John Green\"/>\
<Books>\
<Book author=\"John Green\" title=\"The Fault in Our Stars\"/>\
<Book author=\"Jane Austen\" title=\"Pride &amp; Prejudice\"/>\
<Book author=\"J.D. Salinger\" title=\"Nine Stories\"/>\
</Books>\
</Bookshelf>";
let doc = GXml.Document.from_string (xml);
print ("create_a_document_from_a_string:\n" + doc.to_string (true, 4));
}
function create_a_document_from_a_file () {
let file = Gio.File.new_for_path ("bookshelf.xml");
let can = new Gio.Cancellable ();
let doc = GXml.Document.from_gfile (file, can);
print ("create_a_document_from_a_file:\n" + doc.to_string (true, 4));
}
function create_a_document_from_a_path () {
let doc = GXml.Document.from_path ( "bookshelf.xml");
print ("create_a_document_from_a_path:\n" + doc.to_string (true, 4));
}
function saving_a_document_to_a_path () {
let doc = GXml.Document.from_path ("bookshelf.xml");
doc.save_to_path ("bookshelf2.xml");
}
create_a_document ();
create_a_document_from_a_string ();
create_a_document_from_a_file ();
create_a_document_from_a_path ();
saving_a_document_to_a_path ();
<?xml version="1.0"?>
<Bookshelf>
<Owner fullname="John Green"/>
<Books>
<Book author="John Green" title="The Fault in Our Stars"/>
<Book author="Jane Austen" title="Pride &amp; Prejudice"/>
<Book author="J.D. Salinger" title="Nine Stories"/>
</Books>
</Bookshelf>
#!/usr/bin/python
from gi.repository import GXml;
from gi.repository import Gio;
def create_a_document ():
authors = ["John Green", "Jane Austen", "J.D. Salinger"];
titles = ["The Fault in Our Stars", "Pride & Prejudice", "Nine Stories"];
doc = GXml.Document.new ();
root = doc.create_element ("Bookshelf");
doc.append_child (root);
root.set_attribute ("fullname", "John Green");
books = doc.create_element ("Books");
root.append_child (books);
for i in range (len (authors)):
book = doc.create_element ("Book");
book.set_attribute ("author", authors[i]);
book.set_attribute ("title", titles[i]);
books.append_child (book);
print "create_a_document:\n", doc.to_string (True, 4);
if None == doc.to_string (True, 4):
print "ERROR: doc to string was null, but shouldn't be";
def create_a_document_from_a_string ():
xml = """<?xml version="1.0"?>
<Bookshelf>
<Owner fullname="John Green"/>
<Books>
<Book author="John Green" title="The Fault in Our Stars"/>
<Book author="Jane Austen" title="Pride &amp; Prejudice"/>
<Book author="J.D. Salinger" title="Nine Stories"/>
</Books>
</Bookshelf>""";
doc = GXml.Document.from_string (xml);
print "create_a_document_from_a_string:\n", doc.to_string (True, 4);
# TODO: why does this not indent when printed?
def create_a_document_from_a_file ():
can = Gio.Cancellable ();
gfile = Gio.File.new_for_path ("bookshelf.xml");
doc = GXml.Document.from_gfile (gfile, can);
print "create_a_document_from_a_file:\n", doc.to_string (True, 4); # TODO: do these two values actually do anything?
def create_a_document_from_a_path ():
doc = GXml.Document.from_path ("bookshelf.xml");
print "create_a_document_from_a_path:\n", doc.to_string (True, 4); # TODO: do these two values actually do anything?
def saving_a_document_to_a_path ():
doc = GXml.Document.from_path ("bookshelf.xml");
doc.save_to_path ("bookshelf2.xml");
create_a_document ();
create_a_document_from_a_string ();
create_a_document_from_a_file ();
create_a_document_from_a_path ();
saving_a_document_to_a_path ();
CC=valac
CFLAGS=--vapidir=../../gxml --pkg gxml -o example
example: example.vala
$(CC) $(CFLAGS) example.vala
clean:
rm example
<?xml version="1.0"?>
<Bookshelf>
<Owner fullname="John Green"/>
<Books>
<Book author="John Green" title="The Fault in Our Stars"/>
<Book author="Jane Austen" title="Pride &amp; Prejudice"/>
<Book author="J.D. Salinger" title="Nine Stories"/>
</Books>
</Bookshelf>
using GXml;
void create_a_document () {
string[] authors = { "John Green", "Jane Austen", "J.D. Salinger" };
string[] titles = { "The Fault in Our Stars", "Pride & Prejudice", "Nine Stories" };
try {
Document doc = new Document ();
Element root = doc.create_element ("Bookshelf");
doc.append_child (root);
Element owner = doc.create_element ("Owner");
root.append_child (owner);
owner.set_attribute ("fullname", "John Green");
Element books = doc.create_element ("Books");
root.append_child (books);
for (int i = 0; i < authors.length; i++) {
Element book = doc.create_element ("Book");
book.set_attribute ("author", authors[i]);
book.set_attribute ("title", titles[i]);
books.append_child (book);
}
stdout.printf ("create_a_document:\n%s\n", doc.to_string (true, 8));
} catch (GLib.Error e) {
stderr.printf ("%s\n", e.message);
}
}
void create_a_document_from_a_string () {
string xml;
Document doc;
xml = """<?xml version="1.0"?>
<Bookshelf>
<Owner fullname="John Green"/>
<Books>
<Book author="John Green" title="The Fault in Our Stars"/>
<Book author="Jane Austen" title="Pride &amp; Prejudice"/>
<Book author="J.D. Salinger" title="Nine Stories"/>
</Books>
</Bookshelf>""";
try {
doc = new Document.from_string (xml);
stdout.printf ("create_a_document_from_a_string:\n%s\n", doc.to_string (true, 8));
} catch (GXml.DomError e) {
stderr.printf ("%s\n", e.message);
}
}
void create_a_document_from_a_file () {
File f = File.new_for_path ("bookshelf.xml");
Cancellable can = new Cancellable ();
Document doc;
try {
doc = new Document.from_gfile (f, can);
stdout.printf ("create_a_document_from_a_file:\n%s\n", doc.to_string (true, 8));
} catch (DomError e) {
stderr.printf ("%s\n", e.message);
}
}
void create_a_document_from_a_path () {
Document doc;
try {
doc = new Document.from_path ("bookshelf.xml");
stdout.printf ("create_a_document_from_a_path:\n%s\n", doc.to_string (true, 8));
} catch (DomError e) {
stderr.printf ("%s\n", e.message);
}
}
void saving_a_document_to_a_path () {
Document doc;
try {
doc = new Document.from_path ("bookshelf.xml");
doc.save_to_path ("bookshelf2.xml");
} catch (DomError e) {
stderr.printf ("%s\n", e.message);
}
}
int main (string[] args) {
create_a_document ();
create_a_document_from_a_string ();
create_a_document_from_a_file ();
create_a_document_from_a_path ();
saving_a_document_to_a_path ();
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment