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.

Comments on Reading contents of XML node

Post

Reading contents of XML node

+3
−1

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?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

2 comment threads

libxml tag (3 comments)
General comments (3 comments)
libxml tag
Alexei‭ wrote over 3 years ago

I am trying to write the usage information about the libxml tag and it is not clear if libxml or libxml2 should be used. A quick search suggests libxml2 much more often used and this also seems to be the main tag on SO (libxml is a synonym).

elgonzo‭ wrote over 3 years ago · edited over 3 years ago

@Alexei, i would personally favor to have only "libxml2" and "libxml1" tags instead of "libxml", if possible. While libxml2 is mostly compatible with ye'olde libxml, development packages of the two differ in some aspects that could be relevant for some specific question(s).

Alexei‭ wrote over 3 years ago

elgonzo‭ I have renamed libxml to libxml2 and I will let libxml1 be created should anyone feels a need for it.