Lets say you have the following XML document:
<gtk:Entry Text="This is a label"/>
This can be mapped to:
class UiEntry : GXml.Element {
[Description(nick="::Text")]
public string text { get; set; }
construct {
initialize_with_namespace ("gtk", "https://www.gtk.org/ui", "Entry");
}
}
Now you can use GXml.DomElement.read_from_string()
to read above XML document and the UiEntry.text
property will get the value of the attribute Text
, using the following code:
var e = new UiEntry ();
e.read_from_string("<Gtk:Entry Text=\"This is a label\"/>");
Pay attention to ::Text
, because you should use ::
in order to help GXml
to find the properties you want to map as attributes and what name to use.
Now lets say you have an entry in a container, the concept of container is going away in gtk4, with the code:
<gtk:Box orientation="Horizontal">
<Gtk:Entry Text="This is a label"/>
<Gtk:Entry Text="This is a label"/>
<Gtk:Entry Text="This is a label"/>
</gtk:Box>
First all, we need to define a class for gtk:Box
:
class UiEntry : GXml.Element {
[Description(nick="::orientation")]
public Gtk.Orientation orientation { get; set; }
public UiChildren children { get; set; }
construct {
initialize_with_namespace ("gtk", "https://www.gtk.org/ui", "Box");
child = new UiChildren ();
}
}
The children
property will keep track of your child widgets, so lets define UiChildren
class, as a container where is possible to query a UiEntry
derived class using its property name
as the key:
class UiChildren : GXml.HashMap {
construct {
try { initialize_with_key (typeof (UiEntry),"Name"); }
catch (GLib.Error e) {
warning ("Initialization error for collection type: %s : %s".printf (get_type ().name(), e.message));
}
}
}
Now is possible to read the above XML document using:
var b = new UiBox ();
b.read_from_string("<gtk:Box orientation=\"Horizontal\">
<Gtk:Entry Text=\"This is a label\"/>
<Gtk:Entry Text=\"This is a label\"/>
<Gtk:Entry Text=\"This is a label\"/>
</gtk:Box>");
Above code, will create the set of UiEntry
objects and initialize their properties with the value in the declared attribute.
GXml.Element
use the node’s name to detect a type to use at GLib.Object.new()
to instantiate the object and GLib.Object.set_property()
to find the property with a nickname marked with ::
to map the attributes to object properties.