Here is patch that updates libxml-2.0 bindings to add tree and xpath
API. It requires [Instance(position = n)] patch that I sent yesterday.
o.j.
Sample code:
using Xml;
namespace test {
static int main (string[] args) {
Xml.init();
/* build doc */
var d = new Doc();
var _r = new Node(null, "root");
weak Ns ns = _r.new_ns("http://localhost/myns.dtd",
"myns");
weak Node r = _r;
d.set_root_element(#_r);
/* add nodes */
r.new_child(null, "title", "Hi!");
weak Node a = r.new_child(null, "attrs_test");
a.set_prop("t1", "value of t1");
a.set_ns_prop(ns, "t2", "value of t2 with ns");
weak Node c = r.new_child(ns, "node_with_child");
c.new_child(null, "child", "Child text!");
/* test xmlChar* is freed properly and that string
methods can be used */
xmlstring mem;
int len;
d.dump_memory_format(out mem, ref len, true);
GLib.stdout.printf("len = %d\nstrlen = %d\n\n%s\n", len,
mem.size(), mem);
/* test default args */
mem = null;
d.dump_memory_format(out mem);
GLib.stdout.printf("\n%s\n", mem);
/* test save */
d.save_file("test.xml");
d.save_file_format("test-formatted.xml", true);
/* test Instance(position = 2) */
d.save_file_enc_format("test-formatted-encoded.xml",
"UTF-8", true);
d.set_compress_mode(1);
d.save_file_format("test-formatted-encoded.xml.gz");
/* parse doc */
var d2 = new Doc.read_text_len(mem, len, null, null,
ParserOption.NOERROR |
ParserOption.NOWARNING |
ParserOption.NONET);
if (d2 == null)
return 1;
weak Node root = d2.get_root_element();
for (weak Node iter = root.children; iter != null;
iter = iter.next)
{
GLib.stdout.printf("node %s\n",
iter.get_path());
}
/* xpath */
var ctx = new XPathContext(d2);
d2.order_elements();
var o = ctx.eval("//attrs_test");
if (o != null && o.type == XPathObjectType.NODESET)
{
GLib.stdout.printf("got %d nodes\n",
o.nodeset.length());
for (int i = 0; i < o.nodeset.length(); i++)
GLib.stdout.printf(" -> %s\n",
o.nodeset.item(i).get_path());
}
Xml.cleanup();
return 0;
}
}
Attachment:
0001-libxml-2.0.vapi-add-tree-and-xpath-API-bindings.patch
Description: application/mbox
Attachment:
test.vala
Description: Text Data