Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Reading contents of XML node
I'm writing some functions in C that parses a part of a XML file (using libxml), but instead of extracting the content of the XML node that has a specific name, it outputs a string that's not in UTF-8 or ASCII.
Here's the XML file in question:
<paths>
<file_list>
<group>
<type>config_directory</type>
<file>
<name>AppData/scripts/preferences.empire_script.txt</name>
<sum>5d064a6cfdc35f4714fd31d5d99735d0</sum>
</file>
<file>
<name>preferences</name>
<sum>a0f283d89d97a00011f292cc8f9c0d24</sum>
</file>
</group>
</file_list>
</paths>
This is the first function I call, which parses the XML file and finds the XML node with name "file_list"
int
verify()
{
xmlDoc *doc = NULL;
xmlNode *root_element, *cur_node;
doc = xmlReadFile("test.xml", NULL, 0);
if (doc == NULL)
{
return 1;
}
root_element = xmlDocGetRootElement (doc);
if (root_element->type != XML_ELEMENT_NODE || !xmlStrEqual(root_element->name, (const xmlChar*) "paths"))
{
return 1;
}
cur_node = root_element->children;
while (cur_node != NULL)
{
if (cur_node->type == XML_ELEMENT_NODE && xmlStrEqual(cur_node->name, (const xmlChar*) "file_list"))
{
if (parse_file_list(cur_node, e) != 0) return 1;
}
cur_node = cur_node->next;
}
file_close(f);
return 0;
}
Then I do the same thing in this function and find "group"
static int
parse_file_list(xmlNode *cur)
{
cur = cur->children;
while (cur != NULL)
{
if (cur->type == XML_ELEMENT_NODE && xmlStrEqual(cur->name, (const xmlChar*) "group"))
{
if (parse_group(cur) != 0) return 1;
}
cur = cur->next;
}
return 0;
}
Here, it should parse the contents of the XML node "type", except I just get a segmentation fault when I call xmlNodeGetContent(cur)
static int
parse_group(xmlNode *cur)
{
cur = cur->children;
while (cur != NULL)
{
if (cur->type == XML_ELEMENT_NODE && xmlStrEqual(cur->name, (const xmlChar*) "type"))
{
xmlChar *key = xmlNodeGetContent(cur);
xmlFree(key);
break;
}
cur = cur->next;
}
return 0;
}
Am I missing something?
2 comment threads