diff --git a/vala/libxml-2.0/books.xml b/vala/libxml-2.0/books.xml new file mode 100644 index 0000000..6d8a047 --- /dev/null +++ b/vala/libxml-2.0/books.xml @@ -0,0 +1,11 @@ + + + + The Royal Game + Stefan Zweig + + + The Stoker + Franz Kafka + + diff --git a/vala/libxml-2.0/run.sh b/vala/libxml-2.0/run.sh new file mode 100755 index 0000000..bfe6a3d --- /dev/null +++ b/vala/libxml-2.0/run.sh @@ -0,0 +1,3 @@ +#!/bin/bash + +valac --pkg libxml-2.0 xpath.vala && ./xpath diff --git a/vala/libxml-2.0/xpath.vala b/vala/libxml-2.0/xpath.vala new file mode 100644 index 0000000..1b37a7d --- /dev/null +++ b/vala/libxml-2.0/xpath.vala @@ -0,0 +1,24 @@ +public static int main (string[] args) { + // Parse the document from path + Xml.Doc* doc = Xml.Parser.parse_file ("books.xml"); + if (doc == null) { + stdout.printf ("File 'books.xml' not found or permissions missing\n"); + return 0; + } + + Xml.XPath.Context cntx = new Xml.XPath.Context (doc); + Xml.XPath.Object* res = cntx.eval_expression ("/books/book/title"); + + assert (res != null); + assert (res->type == Xml.XPath.ObjectType.NODESET); + assert (res->nodesetval != null); + + for (int i = 0; i < res->nodesetval->length (); i++) { + Xml.Node* node = res->nodesetval->item (i); + stdout.printf ("%s\n", node->get_content ()); + } + + delete res; + delete doc; + return 0; +}