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 </code> and <code>. Here is a simple XML file format

test.xml

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

test2.xml

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,