How to Read Data From Xml File in C# Console Application

In this article, we are going to discuss how to parse XML in C++ programming language. Nosotros will see several working examples to understand the XML parsing mechanism in C++.

What is XML?

XML is a markup linguistic communication and is mainly used for storing and transferring data in an organized way. XML stands for eXtensible Markup Language. It is very similar to HTML. The XML is completely focused on storing and transferring the data, whereas the HTML is used for displaying the data on the browser.

A Sample XML File/XML Syntax

Here is a sample XML file:

<?xml version="i.0" encoding="utf-8" ?>

<EmployeeData>

<Employee student_type="Part-time" >

<Name> Tom </Proper name>

</Employee>

<Employee student_type="Total-time" >

<Proper noun> Drake </Name>

</Employee>

</EmployeeData>

Unlike HTML, It is a tag-oriented markup linguistic communication, and we can ascertain our own tag in an XML file. In the to a higher place example, we take several user-defined tags such every bit "<Employee>". Every tag will accept the respective catastrophe tag. "</Employee>" is the catastrophe tag for "<Employee>". We tin can define as many user-defined tags as we desire to organize the data.

Parsing Libraries in C++:

There are diverse libraries to parse XML data in most of the high-level programming languages. C++ is non an exception. Here are the near popular C++ libraries to parse XML data:

  1. RapidXML
  2. PugiXML
  3. TinyXML

As the proper name suggests, the RapidXML is mainly focused on speed, and it is a DOM style parsing library. PugiXML supports Unicode conversion. Yous may want to utilize PugiXML if you desire to catechumen UTF-sixteen dr. to UTF-eight. TinyXML is a bare-minimum version to parse XML data and not that fast as compared to the previous two. If you want to simply get the job done and don't care about the speed, you tin cull TinyXML.

Examples
Now, we have a basic agreement of XML and XML parsing libraries in C++. Let'south now await at a couple of examples to parse xml file in C++:

  • Instance-1: Parse XML in C++ using RapidXML
  • Example-ii: Parse XML in C++ using PugiXML
  • Example-iii: Parse XML in C++ using TinyXML

In each of these examples, we volition use the respective libraries to parse a sample XML file.

Example-one: Parse XML in C++ using RapidXML

In this instance program, nosotros will demonstrate how to parse xml using RapidXML library in C++. Here is the input XML file (sample.xml):

<?xml version="one.0" encoding="utf-8" ?>

<MyStudentsData>

<Educatee student_type="Part-time" >

<Proper noun> John</Name>

</Pupil>

<Educatee student_type="Full-time" >

<Proper noun> Sean</Name>

</Student>

<Educatee student_type="Part-time" >

<Name> Sarah</Name>

</Educatee>

</MyStudentsData>

Our goal hither is to parse the above XML file using C++. Hither is the C++ program to parse XML data using RapidXML. Y'all can download the RapidXML library from Here.

#include <iostream>
#include <fstream>
#include <vector>
#include "rapidxml.hpp"

using namespace std;
using namespace rapidxml;

xml_document<> doc
xml_node<> * root_node = NULL ;

int main( void )
{
cout << "\nParsing my students data (sample.xml)....." << endl;

// Read the sample.xml file
ifstream theFile ( "sample.xml" ) ;
vector< char > buffer( (istreambuf_iterator< char > (theFile) ), istreambuf_iterator< char > ( ) ) ;
buffer.push_back ( '\0' ) ;

// Parse the buffer
md.parse < 0 > ( &buffer[ 0 ] ) ;

// Find out the root node
root_node = doctor.first_node ( "MyStudentsData" ) ;

// Iterate over the student nodes
for (xml_node<> * student_node = root_node- >first_node( "Student" ) ; student_node; student_node = student_node- >next_sibling( ) )
{
cout << "\nStudent Type =   " << student_node- >first_attribute( "student_type" ) - >value( ) ;
cout << endl;

// Interate over the Educatee Names
for (xml_node<> * student_name_node = student_node- >first_node( "Proper name" ) ; student_name_node; student_name_node = student_name_node- >next_sibling( ) )
{
cout << "Student Name =   " << student_name_node- >value( ) ;
cout << endl;
}
cout << endl;
}

return 0 ;
}

Example-2: Parse XML in C++ using PugiXML

In this example program, we will demonstrate how to parse xml using PugiXML library in C++. Here is the input XML file (sample.xml):

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>

<EmployeesData FormatVersion="1" >

<Employees>

<Employee Name="John" Type="Function-Time" >

</Employee>

<Employee Proper noun="Sean" Type="Full-Time" >

</Employee>

<Employee Name="Sarah" Type="Function-Time" >

</Employee>

</Employees>

</EmployeesData>

In this example plan, we will demonstrate how to parse xml using pugixml library in C++. You can download the PugiXML library from Here.

#include <iostream>
#include "pugixml.hpp"

using namespace std;
using namespace pugi;

int main( )
{
cout << "\northParsing employees data (sample.xml).....\northward \n" ;

        xml_document dr.;

// load the XML file
if ( !doc.load_file ( "sample.xml" ) ) return - 1 ;

    xml_node tools = dr..child ( "EmployeesData" ).child ( "Employees" ) ;

    for (xml_node_iterator it = tools.begin ( ) ; it ! = tools.stop ( ) ; ++it)
{
cout << "Employees:" ;

for (xml_attribute_iterator ait = it- >attributes_begin( ) ; ait ! = it- >attributes_end( ) ; ++ait)
{
cout << " " << ait- >name( ) << "=" << ait- >value( ) ;
}

cout << endl;
}

cout << endl;

return 0 ;

}

Instance-3: Parse XML in C++ using TinyXML

In this case program, nosotros will demonstrate how to parse xml using TinyXML library in C++. Here is the input XML file (sample.xml):

<?xml version="i.0" encoding="utf-viii" ?>

<MyStudentsData>

<Student> John </Educatee>

<Student> Sean </Student>

<Pupil> Sarah </Pupil>

</MyStudentsData>

In this example plan, we will demonstrate how to parse xml using TinyXML library in C++. Y'all can download the TinyXML library from Hither.

#include <iostream>
#include <fstream>
#include <vector>
#include "tinyxml2.cpp"

using namespace std;
using namespace tinyxml2;

int main( void )
{
cout << "\nParsing my students information (sample.xml)....." << endl;

// Read the sample.xml file
XMLDocument doc;
medico.LoadFile ( "sample.xml" ) ;

const char * title = doc.FirstChildElement ( "MyStudentsData" ) - >FirstChildElement( "Student" ) - >GetText( ) ;
printf ( "Pupil Name: %s\northward", title ) ;

        XMLText* textNode = doc.LastChildElement ( "MyStudentsData" ) - >LastChildElement( "Educatee" ) - >FirstChild( ) - >ToText( ) ;
title = textNode- >Value( ) ;
printf ( "Educatee Proper name: %s\northward", title ) ;

    return 0 ;
}

Determination

In this commodity, we have briefly discussed XML and looked into three different examples of how to parse XML in C++. TinyXML is a minimalistic library for parsing XML data.  Well-nigh of the programmers mainly apply the RapidXML or PugiXML to parse XML information.

Almost the writer

I am a passionate software engineer and blogger. I have done my Masters in Software Engineering from Bits PILANI University, Republic of india. I have very good feel in real-time software evolution and testing using C, C++, and Python. Follow me at thecloudstrap.com.

meyerspatml1943.blogspot.com

Source: https://linuxhint.com/parse_xml_in_cpp/

0 Response to "How to Read Data From Xml File in C# Console Application"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel