How to resolve the algorithm XML/Input step by step in the C programming language
How to resolve the algorithm XML/Input step by step in the C programming language
Table of Contents
Problem Statement
Given the following XML fragment, extract the list of student names using whatever means desired. If the only viable method is to use XPath, refer the reader to the task XML and XPath. Expected Output
Let's start with the solution:
Step by Step solution about How to resolve the algorithm XML/Input step by step in the C programming language
The C program parses an XML document in memory and prints the names of the students in the document using the libxml library.
The program first includes the necessary headers and defines a function print_names
that takes an XML node as input and prints the names of the students in the node's subtree.
The program then defines a constant string buffer
that contains the XML document to be parsed.
The main function creates an XML document object (doc
) and an XML node object (root
) from the buffer
string.
It then calls the print_names
function to print the names of the students in the document.
Finally, it frees the document and node objects and cleans up the parser.
The program also includes a function Unparser
that takes an XML string and a tag name as input and returns a structure containing the tag's attributes and content.
The function Occurs
checks if a substring occurs in a string and the function Get_fn_let
assigns a string to a variable.
The program first checks if the number of arguments is correct and if the input file exists.
It then loads the XML file into a string and parses the string using the Unparser
function.
It then iterates through the attributes of the Student
tags and prints the value of the Name
attribute for each tag.
Finally, it frees the parsed XML string and the input file name.
The program also includes exception handlers for various errors that may occur during execution, such as the XML document not containing any content, the input file not existing, or the number of arguments being incorrect.
Source code in the c programming language
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
static void print_names(xmlNode *node)
{
xmlNode *cur_node = NULL;
for (cur_node = node; cur_node; cur_node = cur_node->next) {
if (cur_node->type == XML_ELEMENT_NODE) {
if ( strcmp(cur_node->name, "Student") == 0 ) {
xmlAttr *prop = NULL;
if ( (prop = xmlHasProp(cur_node, "Name")) != NULL ) {
printf("%s\n", prop->children->content);
}
}
}
print_names(cur_node->children);
}
}
const char *buffer =
"<Students>\n"
" <Student Name=\"April\" Gender=\"F\" DateOfBirth=\"1989-01-02\" />\n"
" <Student Name=\"Bob\" Gender=\"M\" DateOfBirth=\"1990-03-04\" />\n"
" <Student Name=\"Chad\" Gender=\"M\" DateOfBirth=\"1991-05-06\" />\n"
" <Student Name=\"Dave\" Gender=\"M\" DateOfBirth=\"1992-07-08\">\n"
" <Pet Type=\"dog\" Name=\"Rover\" />\n"
" </Student>\n"
" <Student DateOfBirth=\"1993-09-10\" Gender=\"F\" Name=\"Émily\" />\n"
"</Students>\n";
int main()
{
xmlDoc *doc = NULL;
xmlNode *root = NULL;
doc = xmlReadMemory(buffer, strlen(buffer), NULL, NULL, 0);
if ( doc != NULL ) {
root = xmlDocGetRootElement(doc);
print_names(root);
xmlFreeDoc(doc);
}
xmlCleanupParser();
return 0;
}
#include <gadget/gadget.h>
LIB_GADGET_START
Main
Assert( Arg_count == 2, end_input );
Get_arg_str( xml_file, 1 );
Assert( Exist_file(xml_file), file_not_exist );
char* xml = Load_string(xml_file);
ST_GETTAG field = Unparser( &xml, "Students");
Assert ( field.content, fail_content );
while ( Occurs ("Student",field.content ) )
{
ST_GETTAG sub_field = Unparser( &field.content, "Student");
if(sub_field.attrib)
{
int i=0;
Iterator up i [ 0: 1: sub_field.len ]
{
if ( strcmp(sub_field.name[i], "Name" )==0 )
{
Get_fn_let( sub_field.attrib[i], Str_tran( sub_field.attrib[i], "É","É" ) );
/* OK... I must write the function that change this diabolic characters :D */
Print "%s\n",sub_field.attrib[i];
break;
}
}
}
Free tag sub_field;
}
Free tag field;
/* Exceptions areas */
Exception( fail_content ){
Msg_red("Not content for \"Students\" field\n");
}
Free secure xml;
Exception( file_not_exist ){
Msg_redf("File \"%s\" not found\n", xml_file);
}
Free secure xml_file;
Exception( end_input ){
Msg_yellow("Use:\n RC_xml <xml_file.xml>");
}
End
<Students>
<Student Name="April" Gender="F" DateOfBirth="1989-01-02" />
<Student Name="Bob" Gender="M" DateOfBirth="1990-03-04" />
<Student Name="Chad" Gender="M" DateOfBirth="1991-05-06" />
<Student Name="Dave" Gender="M" DateOfBirth="1992-07-08">
<Pet Type="dog" Name="Rover" />
</Student>
<Student DateOfBirth="1993-09-10" Gender="F" Name="Émily" />
</Students>
You may also check:How to resolve the algorithm SEDOLs step by step in the REXX programming language
You may also check:How to resolve the algorithm Dot product step by step in the SQL programming language
You may also check:How to resolve the algorithm Fibonacci sequence step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Sequence of primes by trial division step by step in the Sidef programming language
You may also check:How to resolve the algorithm Monte Carlo methods step by step in the ERRE programming language