Node
Object Hierarchy:
Xml.Node
Xml.Node
Xml.Node
Description:
[ Compact ] [ CCode ( cname = "xmlNode" , free_function = "xmlFreeNode" ) ]public class Node
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:
Methods:
public unowned Node * add_child (unowned Node * cur)
public unowned Node * add_child_list (unowned Node * cur)
public void add_content (string content)
public void add_content_len (string content, int len)
public unowned Node * add_next_sibling (unowned Node * elem)
public unowned Node * add_prev_sibling (unowned Node * elem)
public unowned Node * add_sibling (unowned Node * elem)
public ulong child_element_count ()
public unowned Node * copy (int extended)
public Node copy_list ()
public unowned Attr * copy_prop (unowned Attr * prop)
public unowned Attr * copy_prop_list (unowned Attr * props)
public unowned Node * doc_copy (unowned Doc * doc, int extended)
public unowned Node * doc_copy_list (unowned Doc * doc)
public unowned Node * first_element_child ()
public void free_list ()
public string get_content ()
public string get_lang ()
public unowned Node * get_last_child ()
public long get_line_no ()
public string ? get_no_ns_prop (string name)
public string ? get_ns_prop (string name, string ns)
public string get_path ()
public string ? get_prop (string name)
public int get_space_preserve ()
public unowned Attr * has_ns_prop (string name, string ? name_space)
public unowned Attr * has_prop (string name)
public int is_blank ()
public int is_text ()
public unowned Node * last_element_child ()
public unowned Node * new_child (unowned Ns * ns, string name, string ? content = null )
public unowned Ns * new_ns (string href, string prefix)
public unowned Attr * new_ns_prop (unowned Ns * ns, string name, string value)
public unowned Attr * new_prop (string name, string value)
public unowned Node * new_text_child (unowned Ns * ns, string name, string content)
public unowned Node * next_element_sibling ()
public unowned Node * previous_element_sibling ()
public unowned Node * replace (unowned Node * cur)
public void set_content (string content)
public void set_content_len (string content, int len)
public void set_lang (string val)
public void set_list_doc (unowned Doc * doc)
public void set_name (string name)
public void set_ns (unowned Ns * ns)
public unowned Attr * set_ns_prop (unowned Ns * ns, string name, string ? value)
public unowned Attr * set_prop (string name, string ? value)
public void set_space_preserve (int val)
public void set_tree_doc (unowned Doc * doc)
public int text_concat (string content, int len)
public unowned Node * text_merge (unowned Node * second)
public void unlink ()
public int unset_ns_prop (unowned Ns * ns, string name)
public int unset_prop (string name)
Fields: