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 - For example, a title, can be defined ... 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....
Discussions

Xml parsing in C?
libxml2 is made for C. More on reddit.com
🌐 r/cprogramming
3
2
July 6, 2019
[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 11, 2018
How do I read and parse an XML file in C#? - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... XmlDocument to read an XML from string or from file. More on stackoverflow.com
🌐 stackoverflow.com
🌐
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 - $ gcc -o parse-xml parse-xml.cparse-xml.c:5:27: fatal error: libxml/parser.h: No such file or directory compilation terminated. 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 ...
🌐
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.
#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 214 users
Forked by 71 users
Languages   C 78.9% | C++ 15.0% | CMake 5.2% | Shell 0.9% | C 78.9% | C++ 15.0% | CMake 5.2% | Shell 0.9%
🌐
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.
🌐
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 - The following code creates an instance of the XmlTextReader class and then loads the Books.xml file. Add the following code to the _tmain function: XmlTextReader* reader = new XmlTextReader ("books.xml");
🌐
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
LiveCode LessonsLiveCode LessonsHow To - Step-By-Step Guides To Tasks In LiveCodeWorking with filesHow to read in data from an XML file ... This lesson will show you how to load an XML file and access the data for use in your application. XML files are a very useful for things like storing preference settings, working with the web and for situations where you need to share data with other programs. LiveCode provides a well-featured library for dealing with XML files, which can take a little getting used to but is quite straight forward to use.
Find elsewhere
🌐
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
This c++ example from boost looks promising. Never used it myself. ... Are you using one specific XML file? Is this file going to be changing over a period of time? If so, will you have to deal with the changes? Please answer the questions. in the meantime, you should be able to view the xml file by: 1 copy to desktop 2 change file extention to .txt instead of .xml 3 you may then read contents with a text editor (notepad worked for me) This may give you more insight about what you are up against.
🌐
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! Now that we’ve got the basics down, let’s explore some common XML parsing tasks you might encounter: Reading elements: Extracting data from specific XML elements.
🌐
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 - To read XML data in C/C++, using libraries such as libXML is recommended. This library simplifies the process of parsing XML files and can be found at xmlsoft.org for Unix systems or at zlatkovic.com for Windows.
🌐
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++.

🌐
Oracle
docs.oracle.com › en › database › oracle › oracle-database › 21 › adxdk › using-XML-parser-for-C.html
Using the XML Parser for C
The Oracle XML parser for C reads an XML document and uses DOM or SAX application programming interfaces (APIs) to provide programmatic access to its content and structure. You can use the parser in validating or nonvalidating mode.
🌐
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.

🌐
Quora
quora.com › How-can-I-read-an-xml-file-in-c
How to read an xml file in c
2. You can use their internal namespace as using System.XML 3. If you want to read the XML file, you can use XMLTextReader class as XmlTextReader reader = new XmlTextReader ("filename.xml");//use whole path 4....
🌐
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 "<".