Doc
Object Hierarchy:
Description:
[ Compact ]
[ CCode ( cname = "xmlDoc" , free_function = "xmlFreeDoc" ) ]
public class Doc
Example: Tree: Parse an XML-File:
<?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>
public static void print_simple (Xml.Node* node, string node_name) {
assert (node->name == node_name);
for (Xml.Node* iter = node->children; iter != null; iter = iter->next) {
if (iter->type == Xml.ElementType.TEXT_NODE) {
print (" - %s\n", iter->get_content ());
} else {
print ("Unexpected element %s\n", iter->name);
}
}
}
public static void print_book (Xml.Node* node) {
assert (node->name == "book");
print (" * Book:\n");
string? id = node->get_prop ("id");
if (id != null) {
print (" - %s\n", id);
} else {
print ("Expected: <book id=...\n");
}
for (Xml.Node* iter = node->children; iter != null; iter = iter->next) {
if (iter->type == Xml.ElementType.ELEMENT_NODE) {
switch (iter->name) {
case "title":
print_simple (iter, "title");
break;
case "author":
print_simple (iter, "author");
break;
default:
print ("Unexpected element %s\n", iter->name);
break;
}
}
}
}
public static void print_books (Xml.Node* node) {
assert (node->name == "books");
print ("Books:\n");
for (Xml.Node* iter = node->children; iter != null; iter = iter->next) {
if (iter->type == Xml.ElementType.ELEMENT_NODE) {
if (iter->name == "book") {
print_book (iter);
} else {
print ("Unexpected element %s\n", iter->name);
}
}
}
}
public static int main (string[] args) {
// Parse the document from path
Xml.Doc* doc = Xml.Parser.parse_file ("books.xml");
if (doc == null) {
print ("File 'books.xml' not found or permissions missing\n");
return 0;
}
Xml.Node* root = doc->get_root_element ();
if (root == null) {
print ("WANTED! root\n");
delete doc;
return 0;
}
if (root->name == "books") {
print_books (root);
} else {
print ("Unexpected element %s\n", root->name);
}
delete doc;
return 0;
}
valac --pkg libxml-2.0 parser-dom.vala
Content:
Creation methods:
- public Doc (string? version = null)
Methods:
- public unowned Doc* copy (int recursive)
- public unowned Dtd* create_int_subset (string name, string external_id, string system_id)
- public int dump (FileStream f)
- public int dump_format (FileStream f, bool format = true)
- public void dump_memory (out string mem, out int len = null)
- public void dump_memory_enc (out string mem, out int len = null, string enc = "UTF-8")
- public void dump_memory_enc_format (out string mem, out int len = null, string enc = "UTF-8", bool format = true)
- public void dump_memory_format (out string mem, out int len = null, bool format = true)
- public void elem_dump (FileStream f, unowned Node* cur)
- public int get_compress_mode ()
- public unowned Ns* get_ns_list (unowned Node* node)
- public unowned Node* get_root_element ()
- public unowned Node* new_cdata_block (string content, int len)
- public unowned Node* new_char_ref (string name)
- public unowned Node* new_comment (string content)
- public unowned Node* new_fragment ()
- public unowned Node* new_node (unowned Ns* ns, string name, string? content = null)
- public unowned Node* new_node_eat_name (unowned Ns* ns, owned string name, string? content = null)
- public unowned Node* new_pi (string name, string content)
- public unowned Attr* new_prop (string name, string value)
- public unowned Node* new_raw_node (unowned Ns* ns, string name, string? content = null)
- public unowned Node* new_reference (string name)
- public unowned Node* new_text (string content)
- public unowned Node* new_text_len (string content, int len)
- public string node_list_get_raw_string (unowned Node* list, bool in_line)
- public string node_list_get_string (unowned Node* list, bool in_line)
- public int save_file (string filename)
- public void save_file_enc (string filename, string enc = "UTF-8")
- public int save_format_file (string filename, int format)
- public void save_format_file_enc (string filename, string enc = "UTf-8", bool format = true)
- public unowned Ns* search_ns (unowned Node* node, string? prefix)
- public unowned Ns* search_ns_by_href (unowned Node* node, string? href)
- public void set_compress_mode (int mode)
- public unowned Node* set_root_element (unowned Node* root)
- public unowned Node* string_get_node_list (string str)
- public unowned Node* string_len_get_node_list (string str, int len)
Fields: