diff options
| author | Tomas Bzatek <tbzatek@tbzatek.englab.brq.redhat.com> | 2008-07-30 11:35:37 +0200 |
|---|---|---|
| committer | Tomas Bzatek <tbzatek@tbzatek.englab.brq.redhat.com> | 2008-07-30 11:35:37 +0200 |
| commit | 85cdaaaeafd9eb4ff5e6f6db87e1df5e2e41b431 (patch) | |
| tree | a3ee1caa36d48c87040784d5513515b767dab0f4 | |
| parent | f24a0273cfc6dbf5a74fe11abedba5b172be02a9 (diff) | |
| download | cataract-85cdaaaeafd9eb4ff5e6f6db87e1df5e2e41b431.tar.xz | |
Parse multi-element string values properly (e.g. text[0], CDATA[1], text[2])
Concatenate all matched XPATH elements into one string
| -rw-r--r-- | xml-parser.c | 41 | ||||
| -rw-r--r-- | xml-parser.h | 4 |
2 files changed, 33 insertions, 12 deletions
diff --git a/xml-parser.c b/xml-parser.c index 88373dd..444ea58 100644 --- a/xml-parser.c +++ b/xml-parser.c @@ -81,14 +81,17 @@ xml_parser_close (TXMLFile *file) /* - * xml_file_get_node_value: retrieve value from XPath node + * xml_file_get_node_value: retrieve string value from XPath node + * - multiple matched nodes will be concatenated into one string + * - otherwise please use [0], [1] etc. quantificators */ char * xml_file_get_node_value (TXMLFile *file, const char *x_path) { xmlXPathObjectPtr xpathObj; xmlNodePtr cur; - char *val; + char *val, *valx; + int i; if ((! file) || (! x_path)) return NULL; @@ -102,15 +105,31 @@ xml_file_get_node_value (TXMLFile *file, const char *x_path) val = NULL; if ((xpathObj->nodesetval) && (xpathObj->nodesetval->nodeNr > 0)) { - cur = xpathObj->nodesetval->nodeTab[0]; - - #ifdef __DEBUG_ALL__ - printf("Result (%d nodes):\n", xpathObj->nodesetval->nodeNr); - printf(" XPATH matched: element node \"%s\", value = '%s'\n", cur->name, cur->content); - #endif - - if (cur->content) - val = strdup ((char *) cur->content); + #ifdef __DEBUG_ALL__ + printf("Result (%d nodes):\n", xpathObj->nodesetval->nodeNr); + #endif + + for (i = 0; i < xpathObj->nodesetval->nodeNr; i++) + { + cur = xpathObj->nodesetval->nodeTab[i]; + #ifdef __DEBUG_ALL__ + printf(" XPATH matched: element node \"%s[%d]\", value = '%s'\n", cur->name, i, cur->content); + #endif + + if (cur->content) + { + if (val == NULL) + { + val = g_strdup ((char *) cur->content); + } + else + { + valx = g_strconcat (val, (char *) cur->content, NULL); + g_free (val); + val = valx; + } + } + } } xmlXPathFreeObject (xpathObj); diff --git a/xml-parser.h b/xml-parser.h index 53d7b5a..e130e5b 100644 --- a/xml-parser.h +++ b/xml-parser.h @@ -39,7 +39,9 @@ TXMLFile * xml_parser_load (const char *filename); void xml_parser_close (TXMLFile *file); /* - * xml_file_get_node_value: retrieve value from XPath node + * xml_file_get_node_value: retrieve string value from XPath node + * - multiple matched nodes will be concatenated into one string + * - otherwise please use [0], [1] etc. quantificators */ char * xml_file_get_node_value (TXMLFile *file, const char *x_path); |
