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
test.xml
1 2 3 4 5 |
<FullName> <Title>User Info</Title> <FirstName>Yilmaz</FirstName> <LastName>Yoru</LastName> </FullName> |
To give you more details, here is another xml file example,
test2.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="UTF-8" standalone="no" ?> <!DOCTYPE StockHoldings SYSTEM "sth.dtd"> <StockHoldings> <Stock exchange="NASDAQ"> <name>Borland</name> <price>15.375</price> <symbol>BORL</symbol> <shares>100</shares> </Stock> <Stock exchange="NYSE"> <name>Pfizer</name> <price>42.75</price> <symbol>PFE</symbol> <shares type="preferred">25</shares> </Stock> </StockHoldings> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { FILE* fp; char s[2048]; // Open XML file to read fp = fopen("D:test.xml", "r"); if ( fp == NULL ) printf("File error, can't read! n"); else { printf("Printing file content:n"); // read each lines of XML file while( !feof(fp) ) { fgets(s, 2047, fp ); //max 2047 char printf("%s", s); }; fclose(fp); } getchar(); return 0; } |
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#include <stdio.h> #include <tchar.h> #include <stdlib.h> #include <string.h> int _tmain(int argc, _TCHAR* argv[]) { FILE* fp; char s[2048]; // Open XML file to read fp = fopen("D:test.xml", "r"); if ( fp == NULL ) printf("File error, can't read! n"); else { printf("Printing file content:n"); // read each lines of XML file while( !feof(fp) ) { fgets(s, 2047, fp ); //max 2047 char printf("%s", s); }; fclose(fp); } getchar(); return 0; } |
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,
1 |
char *p1 = strstr(s, "<Title>"); |
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;
- You can create applications with XML Components in C++ Builder. Building an Application with XML Components
- You can create and parse XML documents using Winsoft’s library XML : Create and parse XML documents using Winsoft’s library
- There are more options to use XML files in database applications: Using XML in Database Applications
- You can convert XML documents into data packets too : Converting XML Documents into Data Packets
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++
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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition