summaryrefslogtreecommitdiff
path: root/xml-parser.c
diff options
context:
space:
mode:
authorTomas Bzatek <tbzatek@tbzatek.englab.brq.redhat.com>2008-07-30 11:35:37 +0200
committerTomas Bzatek <tbzatek@tbzatek.englab.brq.redhat.com>2008-07-30 11:35:37 +0200
commit85cdaaaeafd9eb4ff5e6f6db87e1df5e2e41b431 (patch)
treea3ee1caa36d48c87040784d5513515b767dab0f4 /xml-parser.c
parentf24a0273cfc6dbf5a74fe11abedba5b172be02a9 (diff)
downloadcataract-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
Diffstat (limited to 'xml-parser.c')
-rw-r--r--xml-parser.c41
1 files changed, 30 insertions, 11 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);