CC++Learn C++

How To Read An XML File In A C Program Or C++ App

Extensible Markup Language (XML) is one of the most used markup languages in web pages like HTML and used for storing, transmitting, and reconstructing arbitrary data and it is one of the most used file formats in data mining You can easily read XML files in a C or C++ app with the latest C++ version. XML defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. It is a file format in text form, so you can use any text file reading codes on these files.

What is the XML file format?

XML file format means there is a file that uses markups defined by user and written in text format. For example, a title, can be defined between <Title> and </Title>. Here is a simple XML file format


To give you more details, here is another xml file example,


How to read an XML file in a C program

XML is a text form so we can use fopen() function to read (with “R” mode) this file format and we can use fgets() function to read each line of this XML file.

In C++ Builder, simply you can use this code as below too. Just create a Console C application in C++ Builder and modify _tmain() as below,

These are simple applications to read XML files in a C program.

How to search for a string value In an XML file

When you read each line you can search markups of xml by using strstr() function. Here is an example below,

if p1 is not NULL then your string has this markup. Now you can use string operations like strpos(), strtok() and some other pointer operations to get the title there.

How to read an XML file In a C++ app

C++ application has more powerfull and modern options to read XML files. In C++ Builder, XML class is designed to handle this format in operations. For example, XML::XMLDoc can be used in C++ Builder. For more details, please visit this link.

In C++ Builder, TXMLDocument represents an XML document. Use TXMLDocument to represent an XML document. TXMLDocument can read an existing XML document from a file, it can be associated with an in-memory string that is the contents of an XML document, or it can create a new, empty XML document. You can use TXMLDocument directly to load an XML document, read and edit it, and save any changes. You can also use TXMLDocument to access the objects generated by the XML Data Binding wizard.

In C++ Builder, you can use TXMLDocumentLoadFromXML to load XML files in C++ Builder to load XML files, and TXMLDocument.SaveToXML to save XML files.

You can use XML nodes to retrieve data in C++ Builder. Here is an example: Working with XML Nodes

Here are some options to operate on XML Files in C++ Program;

Search in the DocWiki of RADStudio about XML file operations, there are a lot of examples and explanations to read and use XML files in C++

cbuilder 11 512x5121x 7286189 8695819

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?