libxml-2.0 XPath Vala example added.

This commit is contained in:
Kolan Sh 2018-04-25 17:41:12 +03:00
parent 13e3c46bfa
commit 023b85e2d4
3 changed files with 38 additions and 0 deletions

11
vala/libxml-2.0/books.xml Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<books>
<book id="1">
<title>The Royal Game</title>
<author>Stefan Zweig</author>
</book>
<book id="2">
<title>The Stoker</title>
<author>Franz Kafka</author>
</book>
</books>

3
vala/libxml-2.0/run.sh Executable file
View File

@ -0,0 +1,3 @@
#!/bin/bash
valac --pkg libxml-2.0 xpath.vala && ./xpath

View File

@ -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;
}