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 OverflowAnd 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
Is reading the contents of the file into a single, simple buffer really what you want to do? XML files are generally there to be parsed, and you can do this with a library like libxml2, just to give one example (but notably, is implemented in C).
Xml parsing in C?
[C++] Best way to parse XML file?
How to open and read a XML file in C++
How do I read and parse an XML file in C#? - Stack Overflow
Videos
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++.
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.
The title says it all. I have looked at many tutorials and libraries to do it, but it just seems really confusing to me. I know how to do it using ElementTree in Python, so I was thinking in embedding Python code in C++, but I wasn't being sucessful either (when I tried to do it, it gave me an linker error saying it couldn't open "python37.lib")
XmlDocument to read an XML from string or from file.
using System.Xml;
XmlDocument doc = new XmlDocument();
doc.Load("c:\\temp.xml");
or
doc.LoadXml("<xml>something</xml>");
then find a node below it ie like this
XmlNode node = doc.DocumentElement.SelectSingleNode("/book/title");
or
foreach(XmlNode node in doc.DocumentElement.ChildNodes){
string text = node.InnerText; //or loop through its children as well
}
then read the text inside that node like this
string text = node.InnerText;
or read an attribute
string attr = node.Attributes["theattributename"]?.InnerText
Always check for null on Attributes["something"] since it will be null if the attribute does not exist.
LINQ to XML Example:
// Loading from a file, you can also load from a stream
var xml = XDocument.Load(@"C:\contacts.xml");
// Query the data and write out a subset of contacts
var query = from c in xml.Root.Descendants("contact")
where (int)c.Attribute("id") < 4
select c.Element("firstName").Value + " " +
c.Element("lastName").Value;
foreach (string name in query)
{
Console.WriteLine("Contact's Full Name: {0}", name);
}
Reference: LINQ to XML at MSDN
LoadXml is for loading a literal XML string, not for pointing to a file path. Your path is, not surprisingly, not valid XML. You can use xml.Load("...") to load into an XmlDocument from a file.
However, I'd strongly suggest you use LINQ to XML instead unless you have a very good reason to be using the old XmlDocument API. I've made a guess based on your code, though this may not work correctly if your XML isn't structured this way.
var doc = XDocument.Load(@"~\App_Data\Trainings.xml");
var instructorNames = doc.Descendants("Training")
.Elements("name")
.Select(e => e.Value)
.ToList();
You should replace the xml.LoadXml by the method xml.Load because : XmlDocument.Load : is for loading XML either from a stream, TextReader, path/URL, or XmlReader. While the XmlDocument.LoadXml is for loading the XML contained within a string.
so your code should be like that :
xml.Load(@"~\App_Data\Trainings.xml");//Here given xml path location