[gxml.wiki] Update examples for GXml.HashMap writting out XML
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gxml.wiki] Update examples for GXml.HashMap writting out XML
- Date: Mon, 8 Mar 2021 23:43:19 +0000 (UTC)
commit 528e1a9729d02c1bc88b2f038d20c970c8a96ea4
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date: Mon Mar 8 23:43:19 2021 +0000
Update examples for GXml.HashMap writting out XML
examples.md | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 70 insertions(+), 1 deletion(-)
---
diff --git a/examples.md b/examples.md
index 89211fb..6512564 100644
--- a/examples.md
+++ b/examples.md
@@ -252,13 +252,14 @@ class BookStore : GXml.Element
{
[Description (nick="::name")]
public string name { get; set; }
- public Book.Array books { get; set; }
+ public Book.HashMap 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));
}
+ set_instance_property ('books');
}
}
@@ -276,4 +277,72 @@ public class App {
}
```
+## Serialize GObject classes to XML
+
+```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.HashMap 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));
+ }
+ set_instance_property ('books');
+ }
+}
+
+public class App {
+
+ public int main () {
+ var file = GLib.File.new_for_path ("document.xml");
+ BookStore store = new BookStore ();
+ Book book1 = store.books.create_item ();
+ book1.name = "Jay I'm here";
+ store.books.append (book1);
+ Book book2 = store.books.create_item ();
+ book2.name = "GXml is Cool";
+ store.books.append (book2);
+ Book book3 = store.books.create_item ();
+ book3.name = "How explore XML using GXml";
+ store.books.append (book3);
+ store.write_file (file);
+ }
+}
+```
+
+You will have a new `document.xml` file with the following content:
+
+```xml
+<BookStore>
+ <Book name = "Jay I'm here"/>
+ <Book name = "GXml is Cool"/>
+ <Book name = "How explore XML using GXml"/>
+</BookStore>
+```
\ No newline at end of file
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]