libxml-2.0 XPath Vala example added.
This commit is contained in:
parent
13e3c46bfa
commit
023b85e2d4
|
@ -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>
|
|
@ -0,0 +1,3 @@
|
|||
#!/bin/bash
|
||||
|
||||
valac --pkg libxml-2.0 xpath.vala && ./xpath
|
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue