Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Two examples with <a href="http://expat.sourceforge.net" rel="noreferrer">expat</a> and <a href="http://xmlsoft.org/" rel="noreferrer">libxml2</a>. The second one is, IMHO, much easier to use since it creates a tree in memory, a data structure which is easy to work with. expat, on the other hand, does not build anything (you have to do it yourself), it just allows you to call handlers at specific events during the parsing. But expat may be faster (I didn't measure).</p> <p>With expat, reading a XML file and displaying the elements indented:</p> <pre><code>/* A simple test program to parse XML documents with expat &lt;http://expat.sourceforge.net/&gt;. It just displays the element names. On Debian, compile with: gcc -Wall -o expat-test -lexpat expat-test.c Inspired from &lt;http://www.xml.com/pub/a/1999/09/expat/index.html&gt; */ #include &lt;expat.h&gt; #include &lt;stdio.h&gt; #include &lt;string.h&gt; /* Keep track of the current level in the XML tree */ int Depth; #define MAXCHARS 1000000 void start(void *data, const char *el, const char **attr) { int i; for (i = 0; i &lt; Depth; i++) printf(" "); printf("%s", el); for (i = 0; attr[i]; i += 2) { printf(" %s='%s'", attr[i], attr[i + 1]); } printf("\n"); Depth++; } /* End of start handler */ void end(void *data, const char *el) { Depth--; } /* End of end handler */ int main(int argc, char **argv) { char *filename; FILE *f; size_t size; char *xmltext; XML_Parser parser; if (argc != 2) { fprintf(stderr, "Usage: %s filename\n", argv[0]); return (1); } filename = argv[1]; parser = XML_ParserCreate(NULL); if (parser == NULL) { fprintf(stderr, "Parser not created\n"); return (1); } /* Tell expat to use functions start() and end() each times it encounters * the start or end of an element. */ XML_SetElementHandler(parser, start, end); f = fopen(filename, "r"); xmltext = malloc(MAXCHARS); /* Slurp the XML file in the buffer xmltext */ size = fread(xmltext, sizeof(char), MAXCHARS, f); if (XML_Parse(parser, xmltext, strlen(xmltext), XML_TRUE) == XML_STATUS_ERROR) { fprintf(stderr, "Cannot parse %s, file may be too large or not well-formed XML\n", filename); return (1); } fclose(f); XML_ParserFree(parser); fprintf(stdout, "Successfully parsed %i characters in file %s\n", size, filename); return (0); } </code></pre> <p>With libxml2, a program which displays the name of the root element and the names of its children:</p> <pre><code>/* Simple test with libxml2 &lt;http://xmlsoft.org&gt;. It displays the name of the root element and the names of all its children (not descendents, just children). On Debian, compiles with: gcc -Wall -o read-xml2 $(xml2-config --cflags) $(xml2-config --libs) \ read-xml2.c */ #include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;libxml/parser.h&gt; int main(int argc, char **argv) { xmlDoc *document; xmlNode *root, *first_child, *node; char *filename; if (argc &lt; 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 &lt;%s&gt; (%i)\n", root-&gt;name, root-&gt;type); first_child = root-&gt;children; for (node = first_child; node; node = node-&gt;next) { fprintf(stdout, "\t Child is &lt;%s&gt; (%i)\n", node-&gt;name, node-&gt;type); } fprintf(stdout, "...\n"); return 0; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload