And if you want to parse XML, not just reading it into a buffer (something which would not be XML-specific, see Christoph's and Baget's answers), you can use for instance libxml2:

#include <stdio.h>
#include <string.h>
#include <libxml/parser.h>

int main(int argc, char **argv) {
   xmlDoc *document;
   xmlNode *root, *first_child, *node;
   char *filename;

   if (argc < 2) {
     fprintf(stderr, "Usage: %s filename.xml\n", argv[0]);
     return 1;
   }
   filename = argv[1];

  document = xmlReadFile(filename, NULL, 0);
  root = xmlDocGetRootElement(document);
  fprintf(stdout, "Root is <%s> (%i)\n", root->name, root->type);
  first_child = root->children;
  for (node = first_child; node; node = node->next) {
     fprintf(stdout, "\t Child is <%s> (%i)\n", node->name, node->type);
  }
  fprintf(stdout, "...\n");
  return 0;
}

On an Unix machine, you typically compile the above with:

% gcc -o read-xml $(xml2-config --cflags) -Wall $(xml2-config --libs) read-xml.c
Answer from bortzmeyer on Stack Overflow
🌐
Learn C++
learncplusplus.org › home › c++ › how to read an xml file in a c program or c++ app
How To Read An XML File In A C Program Or C++ App
September 3, 2022 - 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...
Discussions

[C++] Best way to parse XML file?
I was going to say you could use a parser library but then I saw the last line. Have you tried looking through stack overflow? The below link should be helpful to you... http://stackoverflow.com/questions/5443073/read-a-line-from-xml-file-using-c More on reddit.com
🌐 r/learnprogramming
18
6
February 26, 2013
How to open and read a XML file in C++
No need to embed python. There are plenty of tools out there. This should give you plenty of options for C++ tools. More on reddit.com
🌐 r/AskProgramming
6
1
July 8, 2018
Xml parsing in C?
libxml2 is made for C. More on reddit.com
🌐 r/cprogramming
3
2
July 6, 2019
SMS Backup and Restore - how to view large backup XML files
I ran into this problem initially and found this excellent software http://www.editpadlite.com/ Was able to open my XML files that were over 1GB More on reddit.com
🌐 r/androidapps
8
2
December 17, 2016
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 243950 › read-xml-from-within-a-c-program
Read XML from within a C Program | DaniWeb
December 6, 2009 - With libxml2 you can parse once, query with XPath, and stream out a new XML doc. ... Read the whole XML once. For each line from your text file, run an XPath that finds the element whose child tag equals the line, then grab the sibling value.
🌐
Physics Forums
physicsforums.com › other sciences › computing and technology
How can I use C/C++ to read XML data structures? • Physics Forums
May 31, 2005 - If you want to learn XML I suggest: www.w3schools.com If you want to read in xml data structures into C/C++ use libXML. http://www.zlatkovic.com/libxml.en.html This link is assuming you run windows.
🌐
LiveCode Lessons
lessons.livecode.com › a › 7011-how-to-read-in-data-from-an-xml-file
How to read in data from an XML file | How To - Step-By-Step Guides To Tasks In LiveCode | LiveCode Lessons
Edit the script of the button by selecting it, then clicking on the "Script" button in the main menu bar. For this example we are going to load an XML file that contains preferences for our application. ... There are two parts to loading the preferences file. The first part is reading the file into memory and creating an XML "tree".
🌐
GitHub
github.com › ooxi › xml.c
GitHub - ooxi/xml.c: Simple XML subset parser comparable to glib's Markup parser, but without any dependencies in one self contained file. · GitHub
#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <xml.h> int main(int argc, char** argv) { /* XML source, could be read from disk */ uint8_t* source = "" "<Root>" "<Hello>World</Hello>" "<This>" "<Is>:-)</Is>" "<An>:-O</An>" "<Example>:-D</Example>" "</This>" "</Root>" ; /* Parse the document * * Watch out: Remember not to free the source until you have freed the * document itself.
Starred by 216 users
Forked by 74 users
Languages   C 78.9% | C++ 15.0% | CMake 5.2% | Shell 0.9%
Find elsewhere
🌐
Microsoft Learn
learn.microsoft.com › en-us › troubleshoot › developer › visualstudio › cpp › language-compilers › read-xml-data-from-file
Read the XML data from a file - Visual C++ | Microsoft Learn
May 8, 2022 - Create an instance of the XmlTextReader object. Populate the XmlTextReader object with the .xml file. Typically, the XmlTextReader class is used if you have to access the raw XML data without the overhead of the DOM.
🌐
Reddit
reddit.com › r/learnprogramming › [c++] best way to parse xml file?
r/learnprogramming on Reddit: [C++] Best way to parse XML file?
February 26, 2013 -

I just got assigned my semestral work. Create XPath interpreter on XML files using C++.

I have general idea of how I will implement the XPath language processing, I have trouble of how to efficiently parse the XML file.

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
</catalog>

Lets say input is file like this. How to efficiently get stuff like one book? My idea is to scan through the file, while searching for '<' and '</' and marking their position in the file. Then somehow parsing all the data inside them into some abstract data structure. I have no idea how though.

Should I just read it char by char until I hit '>' then storing it under some name (author for example) and then reading the actual relevant data?

I can't use any libraries except the standard C/C++ ones.

🌐
W3C
w3.org › XML › 9707 › XML-in-C
XML in C
This is probably too lenient, but since all the delimiters in XML are from the ASCII set, it doesn't really matter. ... All the characters that are allowed in a name, after the first character. The same leniency as for namestart above. ... The data in an XML file, i.e., the characters between a start and end tag, are matched by this regular expression, that accepts all characters except a "<", and only accepts a newline if it is not immediately followed by a "<".
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 154559-how-parse-xml-file-c-having-hard-time-code-example.html
How to parse an XML file in C, having a hard time with code example
February 22, 2013 - Any idea on how to fix this problem? Did I not compile/install the right thing again? ... Well if it didn't install in the default location, you need something like gcc -I/path/to/header/files -o parse-xml parse-xml.c Where the -I parameter is the directory containing libxml/parser.h
🌐
ROOT
root.cern › doc › master › xmlreadfile_8C.html
tutorials/io/xml/xmlreadfile.C File Reference - CERN ROOT
Example to read and parse any xml file, supported by TXMLEngine class The input file, produced by xmlnewfile.C macro is used If you need full xml syntax support, use TXMLParser instead.
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 48957 › read-xml-file-in-c-c
Read XML File in C/C++ | DaniWeb
I suggest you try parsing just one line instead of the whole file, and see what are the contents pointed by pos, and the original array step by step by adding some printf statements. Then try to see how that agrees with the explaination given by that site. You will have a better chance of understanding the code that way. ... Hi please help me, I want to read an xml file with c++. Means, using COM+ logics how I can send my xml file in the local directory to my c++ code to read it?
🌐
HeyCoach Blog
heycoach.in › blog › handling-xml-parsing-with-c-libraries
Handling XML Parsing With C Libraries
December 29, 2024 - In this code snippet, we include the necessary headers, read an XML file, and print the root element. Simple, right? Just like making toast—if toast were made of XML!
🌐
Quora
quora.com › How-do-I-read-a-particular-data-in-xml-file-using-c++
How to read a particular data in xml file using c++ - Quora
Answer: Look at my answer on this question at Manan Joshi's answer to How can I read xml file in c++? There are a lot of libraries present for this which also contains sample code you can look at it to start. If you want to read Xml nodes based on some criteria then read a bit on Xpaths and the...
🌐
ScrapingAnt
scrapingant.com › blog › c-plus-plus-parse-xml
How to Parse XML in C++ | ScrapingAnt
August 5, 2024 - #include "pugixml.hpp" #include <iostream> using namespace pugi; int main() { xml_document doc; xml_parse_result result = doc.load_file("example.xml"); if (!result) { std::cerr << "XML parsed with errors, attr value: [" << doc.child("node").attribute("attr").value() << "]\n" << "Error description: " << result.description() << "\n"; return 1; } xml_node root = doc.child("root"); // Process the XML document // ... return 0; } ... File Reading: xml_parse_result result = doc.load_file("example.xml") loads and parses the XML file.
🌐
Reddit
reddit.com › r/cprogramming › xml parsing in c?
r/cprogramming on Reddit: Xml parsing in C?
July 6, 2019 -

Hello fellow members, I'm currently new to c programming. I need help in parsing a xml file which is metadata file of a program. Which XML library is easy to use in pure C language? It's a commercial application. Please any suggestion.. I searched over internet and find most are c++.