[gxml.wiki] Update examples for GXml.ArrayList



commit 9c88a9d57b14fca692b6b230306d9e0dee83b043
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date:   Mon Mar 8 23:01:53 2021 +0000

    Update examples for GXml.ArrayList

 examples.md | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 62 insertions(+)
---
diff --git a/examples.md b/examples.md
index e4b4814..fece54f 100644
--- a/examples.md
+++ b/examples.md
@@ -120,6 +120,68 @@ public class App {
 }
 ```
 
+## Access child nodes as collections
+
+### Simple list
+
+```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.ArrayList`
+derived class that will recognize and create `Book` classes with the information
+provided by the node.
+
+```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 Array : GXml.ArrayList {
+    construct {
+      try { initialize (typeof (GomBook)); }
+      catch { assert_not_reached (); }
+    }
+  }
+}
+
+class BookStore : GXml.Element
+{
+  [Description (nick="::name")]
+  public string name { get; set; }
+  public Book.Array books { get; set; }
+  construct {
+    try { initialize ("BookStore"); } catch { assert_not_reached (); }
+  }
+}
+
+public class App {
+
+    public int main () {
+        var file = GLib.File.new_for_path ("document.xml");
+        BookStore store = new BookStore ();
+        store.read_from_file (file);
+        for (int i = 0; i < store.books.length; i++) {
+                Book book = store.books.get_item (i) as Book;
+                stdout.printf ("Node name: %s", book.name);
+            }
+        }
+        stdout.printf ("number: %s", inventory.number.to_string ());
+        stdout.printf ("row: %d", inventory.row);
+    }
+}
+```
 
 
  
\ No newline at end of file


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