[gxml/gsoc2013] examples/js: refactor examples into their own files



commit d446425cb88b796f309a965ce1d6ab034780eca1
Author: Richard Schwarting <aquarichy gmail com>
Date:   Sun Jul 28 01:30:11 2013 -0400

    examples/js: refactor examples into their own files

 examples/js/create_document.js             |   34 +++++++++++++
 examples/js/create_document_from_file.js   |   13 +++++
 examples/js/create_document_from_path.js   |   11 ++++
 examples/js/create_document_from_string.js |   21 ++++++++
 examples/js/example.js                     |   70 ----------------------------
 examples/js/save_document_to_path.js       |   11 ++++
 6 files changed, 90 insertions(+), 70 deletions(-)
---
diff --git a/examples/js/create_document.js b/examples/js/create_document.js
new file mode 100755
index 0000000..ee07b23
--- /dev/null
+++ b/examples/js/create_document.js
@@ -0,0 +1,34 @@
+#!/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);
+    }
+}
+
+create_a_document ();
diff --git a/examples/js/create_document_from_file.js b/examples/js/create_document_from_file.js
new file mode 100755
index 0000000..f29eeca
--- /dev/null
+++ b/examples/js/create_document_from_file.js
@@ -0,0 +1,13 @@
+#!/usr/bin/gjs
+
+const GXml = imports.gi.GXml;
+const Gio = imports.gi.Gio;
+
+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));
+}
+
+create_a_document_from_a_file ();
diff --git a/examples/js/create_document_from_path.js b/examples/js/create_document_from_path.js
new file mode 100755
index 0000000..6d4431c
--- /dev/null
+++ b/examples/js/create_document_from_path.js
@@ -0,0 +1,11 @@
+#!/usr/bin/gjs
+
+const GXml = imports.gi.GXml;
+const Gio = imports.gi.Gio;
+
+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));
+}
+
+create_a_document_from_a_path ();
diff --git a/examples/js/create_document_from_string.js b/examples/js/create_document_from_string.js
new file mode 100755
index 0000000..f286af0
--- /dev/null
+++ b/examples/js/create_document_from_string.js
@@ -0,0 +1,21 @@
+#!/usr/bin/gjs
+
+const GXml = imports.gi.GXml;
+const Gio = imports.gi.Gio;
+
+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));
+    
+}
+
+create_a_document_from_a_string ();
diff --git a/examples/js/save_document_to_path.js b/examples/js/save_document_to_path.js
new file mode 100755
index 0000000..b798fc8
--- /dev/null
+++ b/examples/js/save_document_to_path.js
@@ -0,0 +1,11 @@
+#!/usr/bin/gjs
+
+const GXml = imports.gi.GXml;
+const Gio = imports.gi.Gio;
+
+function saving_a_document_to_a_path () {
+    let doc = GXml.Document.from_path ("bookshelf.xml");
+    doc.save_to_path ("bookshelf2.xml");
+}
+
+saving_a_document_to_a_path ();


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