开发者

Accessing parsed xml data

开发者 https://www.devze.com 2023-02-19 19:45 出处:网络
Where does one access the data from a parsed xml file when using libxml? Here is an example usage from xmlsoft

Where does one access the data from a parsed xml file when using libxml?

Here is an example usage from xmlsoft

exampleFunc(const char *filename) {
xmlParserCtxtPtr ctxt; /* the parser context */
xmlDocPtr doc; /* the resulting document tree */

/* create a parser context */
ctxt = xmlNewParserCtxt();
if (ctxt == NULL) {
    fprintf(stderr, "Failed to alloca开发者_JS百科te parser context\n");
return;
}
/* parse the file, activating the DTD validation option */
doc = xmlCtxtReadFile(ctxt, filename, NULL, XML_PARSE_DTDVALID);
/* check if parsing suceeded */
if (doc == NULL) {
    fprintf(stderr, "Failed to parse %s\n", filename);
} else {
/* check if validation suceeded */
    if (ctxt->valid == 0)
    fprintf(stderr, "Failed to validate %s\n", filename);
/* free up the resulting document */
xmlFreeDoc(doc);
}
/* free up the parser context */
xmlFreeParserCtxt(ctxt);
}

How is the tree structure used to get to the data within it?


You can get your data in two ways. In the above code, you already have the xml file parsed and ready in your variable - doc (which is of the type: xmlDocPtr ). Using the functions available in xmlDocPtr module, you can iterate and move through your xml and get the appropriate data. Another method would be to make use of XPath. The library does support XPath and you can make use of this.

Refer to this example: xpath1.c. This makes use of xpath to get data. If you know the your xml, using XPath will be easy and quick way to get the data you need.

0

精彩评论

暂无评论...
验证码 换一张
取 消