From 8916e333a74653cfe6004f69fe47517fb0a020c3 Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Thu, 26 Apr 2018 11:30:22 +0300 Subject: [PATCH] GXml example added. --- vala/gxml/example.vala | 100 +++++++++++++++++++++++++++++++++++++++++ vala/gxml/run.sh | 3 ++ 2 files changed, 103 insertions(+) create mode 100644 vala/gxml/example.vala create mode 100755 vala/gxml/run.sh diff --git a/vala/gxml/example.vala b/vala/gxml/example.vala new file mode 100644 index 0000000..1e45cbf --- /dev/null +++ b/vala/gxml/example.vala @@ -0,0 +1,100 @@ +using GXml; + +void create_a_document () throws GLib.Error { + string[] authors = { "John Green", "Jane Austen", "J.D. Salinger" }; + string[] titles = { "The Fault in Our Stars", "Pride & Prejudice", "Nine Stories" }; + + DomDocument doc = new GomDocument (); + DomElement root = doc.create_element ("Bookshelf"); + doc.append_child (root); + DomElement owner = doc.create_element ("Owner"); + root.append_child (owner); + owner.set_attribute ("fullname", "John Green"); + + DomElement books = doc.create_element ("Books"); + root.append_child (books); + + for (int i = 0; i < authors.length; i++) { + DomElement 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 as GomDocument).write_string ()); +} + +void create_a_document_from_a_string () throws GLib.Error { + string xml; + DomDocument doc; + + xml = """ + + + + + + + +"""; + + doc = new GomDocument.from_string (xml); + stdout.printf ("create_a_document_from_a_string:\n%s\n", (doc as GomDocument).write_string ()); +} + +void create_a_document_from_a_file (string uri) throws GLib.Error { + File f = File.new_for_uri (uri+"/bookshelf2.xml"); + stdout.printf (uri+"\n"); + DomDocument doc; + + doc = new GomDocument.from_file (f); + stdout.printf ("create_a_document_from_a_file:\n%s\n", (doc as GomDocument).write_string ()); +} + +void create_a_document_from_a_path (string uri) throws GLib.Error { + DomDocument doc; + GLib.File f = GLib.File.new_for_uri (uri+"/bookshelf2.xml"); + + doc = new GomDocument.from_path (f.get_path ()); + stdout.printf ("create_a_document_from_a_path:\n%s\n", (doc as GomDocument).write_string ()); +} + +void saving_a_document_to_a_path (string uri) throws GLib.Error { + string xml; + DomDocument doc; + + xml = """ + + + + + + + +"""; + GLib.File f = GLib.File.new_for_uri (uri+"/bookshelf2.xml"); + stdout.printf (f.get_path ()+"\n"); + doc = new GomDocument.from_string (xml); + if (f.query_exists ()) f.delete (); + (doc as GomDocument).write_file (f); + if (!f.query_exists ()) stdout.printf ("Can't save file bookshelf2.xml"); +} + +int main (string[] args) { + GLib.File ex = GLib.File.new_for_path (args[0]); + GLib.File fex = ex.get_parent (); + + try { + create_a_document (); + create_a_document_from_a_string (); + saving_a_document_to_a_path (fex.get_uri ()); + create_a_document_from_a_path (fex.get_uri ()); + create_a_document_from_a_file (fex.get_uri ()); + GLib.File f = GLib.File.new_for_uri (fex.get_uri ()+"/bookshelf2.xml"); + if (f.query_exists ()) f.delete (); + } catch (GLib.Error e) { + warning ("Error: "+e.message); + } + + return 0; +} diff --git a/vala/gxml/run.sh b/vala/gxml/run.sh new file mode 100755 index 0000000..bfbfb23 --- /dev/null +++ b/vala/gxml/run.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +valac --pkg=gxml-0.16 example.vala && ./example