diff options
author | Michael Drake <tlsa@netsurf-browser.org> | 2011-12-22 11:05:48 +0000 |
---|---|---|
committer | Michael Drake <tlsa@netsurf-browser.org> | 2011-12-22 11:05:48 +0000 |
commit | 022a97a640d9ee40da4816e76dbc81bf2a5b0825 (patch) | |
tree | 0e3d812ed13329e45885e3cabddf58b30dac406b /examples | |
parent | 2e69fd126e911be90b1b29e2acc83c1fc0efb2e7 (diff) | |
download | libdom-022a97a640d9ee40da4816e76dbc81bf2a5b0825.tar.gz libdom-022a97a640d9ee40da4816e76dbc81bf2a5b0825.tar.bz2 |
Make example a smidgen more generic. Free the dom_string created for attribute acquisition.
svn path=/trunk/libdom/; revision=13324
Diffstat (limited to 'examples')
-rw-r--r-- | examples/dom-structure-dump.c | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/examples/dom-structure-dump.c b/examples/dom-structure-dump.c index 020033a..f950fb6 100644 --- a/examples/dom-structure-dump.c +++ b/examples/dom-structure-dump.c @@ -130,16 +130,16 @@ dom_document *create_doc_dom_from_file(char *file) /** - * Dump class attribute/value for an element node + * Dump attribute/value for an element node * - * \param node The element node to dump class for + * \param node The element node to dump attribute details for * \return true on success, or false on error */ -bool dump_dom_element_class(dom_node_internal *node) +bool dump_dom_element_attribute(dom_node_internal *node, char *attribute) { dom_exception exc; - dom_string *class = NULL; - dom_string *classvalue = NULL; + dom_string *attr = NULL; + dom_string *attr_value = NULL; dom_node_type type; const char *string; size_t length; @@ -152,32 +152,35 @@ bool dump_dom_element_class(dom_node_internal *node) } assert(type == DOM_ELEMENT_NODE); - /* Create a dom_string containing "class". */ - exc = dom_string_create((uint8_t *)"class", 5, &class); + /* Create a dom_string containing required attribute name. */ + exc = dom_string_create((uint8_t *)attribute, 5, &attr); if (exc != DOM_NO_ERR) { printf(" Exception raised for dom_string_create\n"); return false; } /* Get class attribute's value */ - exc = dom_element_get_attribute(node, class, &classvalue); + exc = dom_element_get_attribute(node, attr, &attr_value); if (exc != DOM_NO_ERR) { printf(" Exception raised for element_get_attribute\n"); return false; - } else if (classvalue == NULL) { - /* Element has no class attribute */ + } else if (attr_value == NULL) { + /* Element lacks required attribute */ return true; } - /* Get attributes's string data */ - string = dom_string_data(classvalue); - length = dom_string_byte_length(classvalue); + /* Finished with the attr dom_string */ + dom_string_unref(attr); - /* Print class info */ - printf(" class=\"%*s\"", (int)length, string); + /* Get attribute value's string data */ + string = dom_string_data(attr_value); + length = dom_string_byte_length(attr_value); - /* Finished with the classvalue dom_string */ - dom_string_unref(classvalue); + /* Print attribute info */ + printf(" %s=\"%*s\"", attribute, (int)length, string); + + /* Finished with the attr_value dom_string */ + dom_string_unref(attr_value); return true; } @@ -236,7 +239,7 @@ bool dump_dom_element(dom_node_internal *node, int depth) dom_string_unref(node_name); /* Print the element's class, if it has one */ - if (dump_dom_element_class(node) == false) { + if (dump_dom_element_attribute(node, "class") == false) { printf("\n"); return false; } |