[gxml.wiki] Create examples
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gxml.wiki] Create examples
- Date: Mon, 8 Mar 2021 21:33:43 +0000 (UTC)
commit 42220e32a2bc7f6c47b57aa14fc4673f4f9e8c05
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date: Mon Mar 8 21:33:43 2021 +0000
Create examples
examples.md | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 125 insertions(+)
---
diff --git a/examples.md b/examples.md
new file mode 100644
index 0000000..e4b4814
--- /dev/null
+++ b/examples.md
@@ -0,0 +1,125 @@
+# Access XML documents using DOM4
+
+GXml allows to read XML documents and access all nodes using [DOM4
API](https://gnome.pages.gitlab.gnome.org/gxml/dev/GXml.DomDocument.html).
+
+## Create a document parsing a file
+```vala
+var file = GLib.File.new_for_path ("document.xml");
+var doc = new GXml.Document.from_file (file);
+```
+
+## Access the Root XML node
+```vala
+var file = GLib.File.new_for_path ("document.xml");
+var doc = new GXml.Document.from_file (file);
+var root = doc.document_element;
+```
+
+## Access to child nodes
+
+Child nodes of `DomElement` could be any like `DomText`, `DomProcessingInstruction`, `DomComment` or other
`DomElement`.
+
+```vala
+var file = GLib.File.new_for_path ("document.xml");
+var doc = new GXml.Document.from_file (file);
+var root = doc.document_element;
+foreach (GXml.DomNode node in root.children) {
+ if (node is GXml.DomElement) {
+ stdout.printf ("Node name: %s", node.node_name);
+ }
+}
+```
+
+## Access to Attributes
+
+A `DomElement` could have attributes with a name and a value as a string.
+
+```vala
+var file = GLib.File.new_for_path ("document.xml");
+var doc = new GXml.Document.from_file (file);
+var root = doc.document_element;
+foreach (GXml.DomNode node in root.children) {
+ if (node is GXml.DomElement) {
+ stdout.printf ("Node name: %s", node.node_name);
+ string val = ((GXml.DomElement) node).get_attribute ("attribute_name");
+ }
+}
+```
+
+# Bind XML to GObject classes
+
+In this example you can bind XML documents like this:
+
+```xml
+<Author>
+ <Name>Jhon</Name>
+ <Email>jhon jhon com</Email>
+</Author>
+```
+
+Here `text_contents` from `GXml.DomNode` will merge all `DomText` child nodes in one string, so take care
not adding any other child nodes like `DomElement`, because will be ignored by that instruction.
+
+```vala
+class Name : GXml.Element
+{
+ construct { try { initialize ("Name"); } catch { assert_not_reached (); } }
+}
+
+class Email : GXml.Element
+{
+ construct { try { initialize ("Email"); } catch { assert_not_reached (); } }
+}
+
+class Author : GXml.Element
+{
+ public Name name { get; set; }
+ public Email email { get; set; }
+ construct { try { initialize ("Author"); } catch { assert_not_reached (); } }
+}
+
+public class App {
+
+ public int main () {
+ var file = GLib.File.new_for_path ("document.xml");
+ Author author = new Author ();
+ author.read_from_file (file);
+ string author_name = author.name.text_contents;
+ }
+}
+```
+
+## Bind XML element's attributes to GObject properties
+
+This implementation, binds following XML document:
+
+```xml
+<Inventory Number="12" Row="33" inventory="33G332TEE"/>
+```
+
+```vala
+class Inventory : GXml.Element
+{
+ [Description (nick="::Number")]
+ public int number { get; set; }
+ [Description (nick="::Row")]
+ public int row { get; set; }
+ [Description (nick="::Inventory")]
+ public string inventory { get; set; }
+ construct { try { initialize ("Inventory"); } catch { assert_not_reached (); } }
+}
+
+public class App {
+
+ public int main () {
+ var file = GLib.File.new_for_path ("document.xml");
+ Inventory inventory = new Inventory ();
+ inventory.read_from_file (file);
+ 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]