Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

Post History

62%
+3 −1
Q&A 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 UT...

0 answers  ·  posted 3y ago by southernisles‭  ·  edited 3y ago by Alexei‭

#5: Post edited by user avatar Alexei‭ · 2021-07-06T11:37:04Z (almost 3 years ago)
added relevant tag
#4: Post edited by user avatar Alexei‭ · 2021-06-06T05:25:50Z (almost 3 years ago)
added relevant tags
#3: Post edited by user avatar southernisles‭ · 2021-06-05T17:22:30Z (almost 3 years ago)
  • 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)
  • {
  • char *type = NULL;
  • cur = cur->children;
  • while (cur != NULL)
  • {
  • if (cur->type == XML_ELEMENT_NODE && xmlStrEqual(cur->name, (const xmlChar*) "type"))
  • {
  • xmlChar *key = xmlNodeGetContent(cur);
  • if (key == NULL) {
  • return 1;
  • }
  • if (conf_verify_key(conf, (const char*) key, type) > 0) {
  • return 1;
  • }
  • xmlFree(key);
  • break;
  • }
  • cur = cur->next;
  • }
  • while (cur != NULL)
  • {
  • if (cur->type == XML_ELEMENT_NODE && xmlStrEqual(cur->name, (const xmlChar*) "file"))
  • {
  • if (parse_file(cur, type) != 0) return 1;
  • }
  • cur = cur->next;
  • }
  • free(type);
  • return 0;
  • }
  • ```
  • Am I missing something?
  • 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: Post edited by user avatar southernisles‭ · 2021-06-05T17:05:15Z (almost 3 years ago)
  • 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
  • ```
  • static int
  • parse_group(xmlNode *cur)
  • {
  • char *type = NULL;
  • cur = cur->children;
  • while (cur != NULL)
  • {
  • if (cur->type == XML_ELEMENT_NODE && xmlStrEqual(cur->name, (const xmlChar*) "type"))
  • {
  • xmlChar *key = xmlNodeGetContent(cur);
  • if (key == NULL) {
  • return 1;
  • }
  • if (conf_verify_key(conf, (const char*) key, type) > 0) {
  • return 1;
  • }
  • xmlFree(key);
  • break;
  • }
  • cur = cur->next;
  • }
  • while (cur != NULL)
  • {
  • if (cur->type == XML_ELEMENT_NODE && xmlStrEqual(cur->name, (const xmlChar*) "file"))
  • {
  • if (parse_file(cur, type) != 0) return 1;
  • }
  • cur = cur->next;
  • }
  • free(type);
  • return 0;
  • }
  • ```
  • Am I missing something?
  • 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)
  • {
  • char *type = NULL;
  • cur = cur->children;
  • while (cur != NULL)
  • {
  • if (cur->type == XML_ELEMENT_NODE && xmlStrEqual(cur->name, (const xmlChar*) "type"))
  • {
  • xmlChar *key = xmlNodeGetContent(cur);
  • if (key == NULL) {
  • return 1;
  • }
  • if (conf_verify_key(conf, (const char*) key, type) > 0) {
  • return 1;
  • }
  • xmlFree(key);
  • break;
  • }
  • cur = cur->next;
  • }
  • while (cur != NULL)
  • {
  • if (cur->type == XML_ELEMENT_NODE && xmlStrEqual(cur->name, (const xmlChar*) "file"))
  • {
  • if (parse_file(cur, type) != 0) return 1;
  • }
  • cur = cur->next;
  • }
  • free(type);
  • return 0;
  • }
  • ```
  • Am I missing something?
#1: Initial revision by user avatar southernisles‭ · 2021-06-05T16:38:02Z (almost 3 years ago)
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

```
static int
parse_group(xmlNode *cur)
{
  char *type = NULL;
  cur = cur->children;
  while (cur != NULL)
    {
      if (cur->type == XML_ELEMENT_NODE && xmlStrEqual(cur->name, (const xmlChar*) "type"))
	  {
	    xmlChar *key = xmlNodeGetContent(cur);
	   
	    if (key == NULL) {
	       return 1;
	    }
	    if (conf_verify_key(conf, (const char*) key, type) > 0) {
	      return 1;
	    }
	    xmlFree(key);
	    break;
	  }
      cur = cur->next;
    }

  while (cur != NULL)
    {
      if (cur->type == XML_ELEMENT_NODE && xmlStrEqual(cur->name, (const xmlChar*) "file"))
	  {
	    if (parse_file(cur, type) != 0) return 1;
	  }
      cur = cur->next;
    }
  free(type);
  return 0;
}
```

Am I missing something?