[gxml.wiki] Update examples for GXml.HashMap



commit ef824413c514eccf47d6407aebb570f22e338150
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date:   Mon Mar 8 23:22:45 2021 +0000

    Update examples for GXml.HashMap

 examples.md | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 93 insertions(+), 5 deletions(-)
---
diff --git a/examples.md b/examples.md
index fece54f..ba0e820 100644
--- a/examples.md
+++ b/examples.md
@@ -62,7 +62,13 @@ Here `text_contents` from `GXml.DomNode` will merge all `DomText` child nodes in
 ```vala
 class Name : GXml.Element
 {
-  construct { try { initialize ("Name"); } catch { assert_not_reached (); } }
+  construct {
+      try { initialize ("Name"); }
+      catch (GLib.Error e) {
+        warning ("Initialization error for collection type: %s : %s"
+            .printf (get_type ().name(), e.message));
+      }
+  }
 }
 
 class Email : GXml.Element
@@ -74,7 +80,13 @@ class Author : GXml.Element
 {
   public Name name { get; set; }
   public Email email { get; set; }
-  construct { try { initialize ("Author"); } catch { assert_not_reached (); } }
+  construct {
+      try { initialize ("Author"); }
+      catch (GLib.Error e) {
+        warning ("Initialization error for collection type: %s : %s"
+            .printf (get_type ().name(), e.message));
+      }
+  }
 }
 
 public class App {
@@ -105,7 +117,13 @@ class Inventory : GXml.Element
   public int row { get; set; }
   [Description (nick="::Inventory")]
   public string inventory { get; set; }
-  construct { try { initialize ("Inventory"); } catch { assert_not_reached (); } }
+  construct {
+      try { initialize ("Inventory"); }
+      catch (GLib.Error e) {
+        warning ("Initialization error for collection type: %s : %s"
+            .printf (get_type ().name(), e.message));
+      }
+  }
 }
 
 public class App {
@@ -151,7 +169,10 @@ class Book : GXml.Element
   public class Array : GXml.ArrayList {
     construct {
       try { initialize (typeof (GomBook)); }
-      catch { assert_not_reached (); }
+      catch (GLib.Error e) {
+        warning ("Initialization error for collection type: %s : %s"
+            .printf (get_type ().name(), e.message));
+      }
     }
   }
 }
@@ -162,7 +183,11 @@ class BookStore : GXml.Element
   public string name { get; set; }
   public Book.Array books { get; set; }
   construct {
-    try { initialize ("BookStore"); } catch { assert_not_reached (); }
+      try { initialize ("BookStore"); }
+      catch (GLib.Error e) {
+        warning ("Initialization error for collection type: %s : %s"
+            .printf (get_type ().name(), e.message));
+      }
   }
 }
 
@@ -182,6 +207,69 @@ public class App {
     }
 }
 ```
+### Hash Map
+
+```xml
+<BookStore>
+   <Book name = "Jay I'm here"/>
+   <Book name = "GXml is Cool"/>
+   <Book name = "How explore XML using GXml"/>
+</BookStore>
+```
+
+Above XML has a set of children, all of them are books, so we will use `GXml.HashMap`
+derived class that will recognize and create `Book` classes with the information
+provided by the node, but also we can access each book using its name as a key in a hash table.
+
+```vala
+class Book : GXml.Element
+{
+  [Description(nick="::Name")]
+  public string name { get; set; }
+  [Description(nick="::Year")]
+  public string year { get; set; }
+  [Description(nick="::ISBN")]
+  public string isbn { get; set; }
+  construct {
+    try { initialize ("Book"); } catch { assert_not_reached (); }
+  }
+  public class HashMap : GXml.HashMap {
+    construct {
+      try { initialize_with_key (typeof (Book),"Name"); }
+      catch (GLib.Error e) {
+        warning ("Initialization error for collection type: %s : %s"
+            .printf (get_type ().name(), e.message));
+      }
+    }
+  }
+}
+
+class BookStore : GXml.Element
+{
+  [Description (nick="::name")]
+  public string name { get; set; }
+  public Book.Array books { get; set; }
+  construct {
+      try { initialize ("BookStore"); }
+      catch (GLib.Error e) {
+        warning ("Initialization error for collection type: %s : %s"
+            .printf (get_type ().name(), e.message));
+      }
+  }
+}
 
+public class App {
+
+    public int main () {
+        var file = GLib.File.new_for_path ("document.xml");
+        BookStore store = new BookStore ();
+        store.read_from_file (file);
+        Book b = store.books.get ("GXml is Cool") as Book;
+        if (b != null) {
+            stdout.printf ("Book's name: %s", book.name);;
+        }
+    }
+}
+```
 
  
\ No newline at end of file


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