* @filename: file to be parsed or NULL if string parsing is requested
* @xmlStr: XML string to be parsed in case filename is NULL
* @url: URL of XML document for string parser
+ * @rootelement: Optional name of the expected root element
* @ctxt: optional pointer to populate with new context pointer
*
* Parse XML document provided either as a file or a string. The function
* guarantees that the XML document contains a root element.
*
+ * If @rootelement is not NULL, the name of the root element of the parsed XML
+ * is vaidated against
+ *
* Returns parsed XML document.
*/
xmlDocPtr
const char *filename,
const char *xmlStr,
const char *url,
+ const char *rootelement,
xmlXPathContextPtr *ctxt)
{
struct virParserData private;
g_autoptr(xmlParserCtxt) pctxt = NULL;
g_autoptr(xmlDoc) xml = NULL;
+ xmlNodePtr rootnode;
const char *docname;
if (filename)
return NULL;
}
- if (xmlDocGetRootElement(xml) == NULL) {
+ if (!(rootnode = xmlDocGetRootElement(xml))) {
virGenericReportError(domcode, VIR_ERR_INTERNAL_ERROR,
"%s", _("missing root element"));
return NULL;
}
+ if (rootelement &&
+ !virXMLNodeNameEqual(rootnode, rootelement)) {
+ virReportError(VIR_ERR_XML_ERROR,
+ _("expecting root element of '%s', not '%s'"),
+ rootelement, rootnode->name);
+ return NULL;
+ }
+
if (ctxt) {
if (!(*ctxt = virXMLXPathContextNew(xml)))
return NULL;
- (*ctxt)->node = xmlDocGetRootElement(xml);
+ (*ctxt)->node = rootnode;
}
return g_steal_pointer(&xml);
const char *filename,
const char *xmlStr,
const char *url,
+ const char *rootelement,
xmlXPathContextPtr *ctxt);
const char *
* Return the parsed document object, or NULL on failure.
*/
#define virXMLParse(filename, xmlStr, url) \
- virXMLParseHelper(VIR_FROM_THIS, filename, xmlStr, url, NULL)
+ virXMLParseHelper(VIR_FROM_THIS, filename, xmlStr, url, NULL, NULL)
/**
* virXMLParseString:
* Return the parsed document object, or NULL on failure.
*/
#define virXMLParseString(xmlStr, url) \
- virXMLParseHelper(VIR_FROM_THIS, NULL, xmlStr, url, NULL)
+ virXMLParseHelper(VIR_FROM_THIS, NULL, xmlStr, url, NULL, NULL)
/**
* virXMLParseFile:
* Return the parsed document object, or NULL on failure.
*/
#define virXMLParseFile(filename) \
- virXMLParseHelper(VIR_FROM_THIS, filename, NULL, NULL, NULL)
+ virXMLParseHelper(VIR_FROM_THIS, filename, NULL, NULL, NULL, NULL)
/**
* virXMLParseCtxt:
* Return the parsed document object, or NULL on failure.
*/
#define virXMLParseCtxt(filename, xmlStr, url, pctxt) \
- virXMLParseHelper(VIR_FROM_THIS, filename, xmlStr, url, pctxt)
+ virXMLParseHelper(VIR_FROM_THIS, filename, xmlStr, url, NULL, pctxt)
/**
* virXMLParseStringCtxt:
* Return the parsed document object, or NULL on failure.
*/
#define virXMLParseStringCtxt(xmlStr, url, pctxt) \
- virXMLParseHelper(VIR_FROM_THIS, NULL, xmlStr, url, pctxt)
+ virXMLParseHelper(VIR_FROM_THIS, NULL, xmlStr, url, NULL, pctxt)
/**
* virXMLParseFileCtxt:
* Return the parsed document object, or NULL on failure.
*/
#define virXMLParseFileCtxt(filename, pctxt) \
- virXMLParseHelper(VIR_FROM_THIS, filename, NULL, NULL, pctxt)
+ virXMLParseHelper(VIR_FROM_THIS, filename, NULL, NULL, NULL, pctxt)
int
virXMLSaveFile(const char *path,