1
0
mirror of https://github.com/OpenNebula/one.git synced 2025-02-27 13:57:23 +03:00

Feature #1554: New method ObjectXML::add_node

This commit is contained in:
Carlos Martín 2013-01-22 14:14:56 +01:00
parent 5bc0ae8634
commit afc4cddfd5
2 changed files with 71 additions and 0 deletions

View File

@ -148,6 +148,18 @@ public:
*/
int get_nodes(const char * xpath_expr, vector<xmlNodePtr>& content);
/**
* Adds a copy of the node as a child of the node in the xpath expression.
* The source node must be cleaned by the caller.
*
* @param xpath_expr Path of the parent node
* @param node Node copy and add
* @param new_name New name for the node copy
*
* @return 0 on success, -1 otherwise
*/
int add_node(const char * xpath_expr, xmlNodePtr node, const char * new_name);
/**
* Frees a vector of XMLNodes, as returned by the get_nodes function
* @param content the vector of xmlNodePtr

View File

@ -375,6 +375,65 @@ int ObjectXML::get_nodes (const char * xpath_expr, vector<xmlNodePtr>& content)
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ObjectXML::add_node(
const char * xpath_expr,
xmlNodePtr node,
const char * new_name)
{
xmlXPathObjectPtr obj;
vector<string> content;
obj = xmlXPathEvalExpression(
reinterpret_cast<const xmlChar *>(xpath_expr), ctx);
if (obj == 0 || obj->nodesetval == 0)
{
return -1;
}
xmlNodeSetPtr ns = obj->nodesetval;
int size = ns->nodeNr;
xmlNodePtr cur;
for(int i = 0; i < size; ++i)
{
cur = ns->nodeTab[i];
if ( cur == 0 || cur->type != XML_ELEMENT_NODE )
{
continue;
}
xmlNodePtr node_cpy = xmlCopyNode(node, 1);
if (node_cpy == 0)
{
xmlXPathFreeObject(obj);
return -1;
}
xmlNodeSetName(node_cpy, reinterpret_cast<const xmlChar *>(new_name));
xmlNodePtr res = xmlAddChild(cur, node_cpy);
if (res == 0)
{
xmlXPathFreeObject(obj);
xmlFreeNode(node_cpy);
return -1;
}
}
xmlXPathFreeObject(obj);
return 0;
}
/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */
int ObjectXML::update_from_str(const string &xml_doc)
{
if (xml != 0)