Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8141843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:38:45+00:00 2026-06-06T12:38:45+00:00

I’m new to C programming. I want to create C program which can read

  • 0

I’m new to C programming. I want to create C program which can read XML file using libxml2.
I tested this code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>

// Declare functions
void parseSystemProperties(char *xmlFileName);
void parseSystemModules(xmlDocPtr doc, xmlNodePtr cur);
void parseSystemConfiguration(xmlDocPtr doc, xmlNodePtr cur);
void parseProfiles(xmlDocPtr doc, xmlNodePtr cur);
void parseMonitoringPort(xmlDocPtr doc, xmlNodePtr cur);
void parseWebServicePort(xmlDocPtr doc, xmlNodePtr cur);
void parseBindingInterfaces(xmlDocPtr doc, xmlNodePtr cur);
void parseExtensions(xmlDocPtr doc, xmlNodePtr cur);

int main(int argc, char **argv) {
    char *xmlFileName;

    if (argc <= 1) {
        printf("Usage: %s inputfile.xml\n", argv[0]);
        return (0);
    }

    // Get the file name from the argv[1]
    xmlFileName = argv[1];

    // Custom function to parse XML file
    parseSystemProperties(xmlFileName);

    return (1);
}

// Parsing the XML file and Reading the Element Nodes

void parseSystemProperties(char *xmlFileName) {
    xmlDocPtr doc; // pointer to parse xml Document
    xmlNodePtr cur; // node pointer. It interacts with individual node

    // Parse XML file
    doc = xmlParseFile(xmlFileName);

    // Check to see that the document was successfully parsed.
    if (doc == NULL) {
        fprintf(stderr, "Error!. Document is not parsed successfully. \n");
        return;
    }

    // Retrieve the document's root element - system-properties
    cur = xmlDocGetRootElement(doc);

    // Check to make sure the document actually contains something
    if (cur == NULL) {
        fprintf(stderr, "Document is Empty\n");
        xmlFreeDoc(doc);
        return;
    }

    /* We need to make sure the document is the right type.
     * "system-properties" is the root type of the documents used in user Config XML file
     */
    if (xmlStrcmp(cur->name, (const xmlChar *) "system-properties")) {
        fprintf(stderr, "Cannot find system-properties");
        xmlFreeDoc(doc);
        return;
    }

    /* Get the first child node of cur.
     * At this point, cur points at the document root,
     * which is the element "root"
     */

    cur = cur->xmlChildrenNode;

    // This loop iterates through the elements that are children of "root"
    while (cur != NULL) {
        if ((!xmlStrcmp(cur->name, (const xmlChar *) "system-modules"))) {
            parseSystemModules(doc, cur);
        }
        cur = cur->next;
    }


    cur = cur->xmlChildrenNode;

    // This loop iterates through the elements that are children of "root"
    while (cur != NULL) {
        if ((!xmlStrcmp(cur->name, (const xmlChar *) "system-configuration"))) {
            parseSystemConfiguration(doc, cur);
        }
        cur = cur->next;
    }

    /* Save XML document to the Disk
     * Otherwise, you changes will not be reflected to the file.
     * Currently it's only in the memory
     */
    // xmlSaveFormatFile (xmlFileName, doc, 1);

    /* free the document */
    xmlFreeDoc(doc);

    /*
     * Free the global variables that may
     * have been allocated by the parser.
     */
    xmlCleanupParser();

    return;

} // end of function

// Get Modules part
// -------------------------------------------

void parseSystemModules(xmlDocPtr doc, xmlNodePtr cur) {
    xmlChar *key;
    xmlAttrPtr attr;

    // Get the sub Element Node of system-configuration node
    cur = cur->xmlChildrenNode;

    // This loop iterates through the elements that are children of system-configuration
    while (cur != NULL) {
        if ((!xmlStrcmp(cur->name, (const xmlChar *) "extensions"))) {
            parseExtensions(doc, cur);
        }
        cur = cur->next;
    }

    return;

} // end of function()

void parseExtensions(xmlDocPtr doc, xmlNodePtr cur) {
    xmlChar *key;
    xmlAttrPtr attr;

    // Get the sub Element Node of Profiles node
    cur = cur->xmlChildrenNode;

    // This loop iterates through the elements that are children of Profiles
    while (cur != NULL) {
        if ((!xmlStrcmp(cur->name, (const xmlChar *) "extension"))) {
            key = xmlGetProp(cur, (const xmlChar*) "module");
            fprintf(stderr, "module: %s\n", key);
            xmlFree(key);
        }
        cur = cur->next;
    }

    return;

} // end of function()



// Get Configuration part
// -------------------------------------------

void parseSystemConfiguration(xmlDocPtr doc, xmlNodePtr cur) {
    xmlChar *key;
    xmlAttrPtr attr;

    // Get the sub Element Node of system-configuration node
    cur = cur->xmlChildrenNode;

    // This loop iterates through the elements that are children of system-configuration
    while (cur != NULL) {
        if ((!xmlStrcmp(cur->name, (const xmlChar *) "profiles"))) {
            parseProfiles(doc, cur);
        }
        cur = cur->next;
    }

    return;

} // end of function()

void parseProfiles(xmlDocPtr doc, xmlNodePtr cur) {
    xmlChar *key;
    xmlAttrPtr attr;

    // Get the sub Element Node of Profiles node
    cur = cur->xmlChildrenNode;

    // This loop iterates through the elements that are children of Profiles
    while (cur != NULL) {
        if ((!xmlStrcmp(cur->name, (const xmlChar *) "profile"))) {
            // Get monitoring-port
            parseMonitoringPort(doc, cur);
            // Get web-service-port
            parseWebServicePort(doc, cur);
            parseBindingInterfaces(doc, cur);
        }
        cur = cur->next;
    }

    return;

} // end of function()


// Monitoring Port

void parseMonitoringPort(xmlDocPtr doc, xmlNodePtr cur) {
    xmlChar *key;
    xmlAttrPtr attr;

    // Get the sub Element Node of Profile node
    cur = cur->xmlChildrenNode;

    // This loop iterates through the elements that are children of Profile
    while (cur != NULL) {
        if ((!xmlStrcmp(cur->name, (const xmlChar *) "monitoring-port"))) {
            key = xmlGetProp(cur, (const xmlChar*) "port");
            fprintf(stderr, "monitoring-port: %s\n", key);
            xmlFree(key);
        }
        cur = cur->next;
    }

    return;

} // end of function()


// web service port

void parseWebServicePort(xmlDocPtr doc, xmlNodePtr cur) {
    xmlChar *key;
    xmlAttrPtr attr;

    // Get the sub Element Node of Profile node
    cur = cur->xmlChildrenNode;

    // This loop iterates through the elements that are children of "root"
    while (cur != NULL) {
        if ((!xmlStrcmp(cur->name, (const xmlChar *) "web-service-port"))) {
            //parseMonitoringPort (doc, cur);
            key = xmlGetProp(cur, (const xmlChar*) "port");
            fprintf(stderr, "web-service-port: %s\n", key);
            xmlFree(key);
        }
        cur = cur->next;
    }

    return;

} // end of function()


// binding interface

void parseBindingInterfaces(xmlDocPtr doc, xmlNodePtr cur) {
    xmlChar *key;
    xmlAttrPtr attr;

    // Get the sub Element Node of Profile node
    cur = cur->xmlChildrenNode;

    // This loop iterates through the elements that are children of "root"
    while (cur != NULL) {
        if ((!xmlStrcmp(cur->name, (const xmlChar *) "socket-binding"))) {
            //parseMonitoringPort (doc, cur);
            key = xmlGetProp(cur, (const xmlChar*) "interface");
            fprintf(stderr, "interface: %s\n", key);
            xmlFree(key);
        }
        cur = cur->next;
    }

    return;

} // end of function()

This is the XML that I want to read:

<?xml version="1.0" encoding="UTF-8"?>
<system-properties version="1.0">
    <system-modules>
        <extensions>
            <extension module="NetworkModule"/>
            <extension module="MonitoringModule"/>
            <extension module="OracleModule"/>
            <extension module="DataFilterModule"/>          
        </extensions>   
    </system-modules>
    <system-configuration>
        <profiles>
            <profile name="default">
                <monitoring-port port="6051"/>
                <web-service-port port="7050"/>
                <socket-binding interface="management" ipaddress="192.168.1.101" port="6050"/>
                <socket-binding interface="monitoring" ipaddress="192.168.1.106" port="7050"/>
                <network-pool threads="40"/>                    
            </profile>
        </profiles>
    </system-configuration>
</system-properties>

Unfortunately I get this error when I run the example:

[rcbandit@Laptop Ctest]$ ./test xmlfile.xml
module: NetworkModule
module: MonitoringModule
module: OracleModule
module: DataFilterModule
Segmentation fault (core dumped)
[user@Laptop Ctest]$ 

When I remove this line the code works:

cur = cur->xmlChildrenNode;

    // This loop iterates through the elements that are children of "root"
    while (cur != NULL) {
        if ((!xmlStrcmp(cur->name, (const xmlChar *) "system-modules"))) {
            parseSystemModules(doc, cur);
        }
        cur = cur->next;
    }

Can you help me to fix this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:38:46+00:00Added an answer on June 6, 2026 at 12:38 pm

    Problem is here:

    // This loop iterates through the elements that are children of "root"
    while (cur != NULL) {
        if ((!xmlStrcmp(cur->name, (const xmlChar *) "system-modules"))) {
            parseSystemModules(doc, cur);
        }
        cur = cur->next;
    }
    
    
    cur = cur->xmlChildrenNode;
    

    The while exits when cur is NULL & dereference it next statement viz. cur = cur->xmlChildrenNode; thus the crash in cur->xmlChildrenNode.
    Maybe you can try to do the comparisons in the single loop maybe on these lines:

    while (cur != NULL) {
        if ((!xmlStrcmp(cur->name, (const xmlChar *) "system-modules"))) {
            parseSystemModules(doc, cur);
        }
        else if ((!xmlStrcmp(cur->name, (const xmlChar *) "system-configuration"))) {
            parseSystemConfiguration(doc, cur);
        }
        cur = cur->next;
    }
    

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I want use html5's new tag to play a wav file (currently only supported
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I am using parse_ini_file to read the contents of a file however it is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the
Does anyone know how can I replace this 2 symbol below from the string
I'm parsing an XML file, the creators of it stuck in a bunch social

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.