Skip to content

Commit

Permalink
Add: XML element_to_string function
Browse files Browse the repository at this point in the history
  • Loading branch information
timopollmeier committed Apr 4, 2023
2 parents ff407a6 + dcc5de3 commit ba1b52a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
30 changes: 30 additions & 0 deletions util/xmlutils.c
Expand Up @@ -1919,3 +1919,33 @@ element_next (element_t element)
return element->next;
return NULL;
}

/**
* @brief Output the XML element as a string.
*
* The generated XML string will include namespace definitions from ancestor
* elements.
*
* @param[in] element The element to output as a string.
*
* @return The newly allocated XML string.
*/
gchar *
element_to_string (element_t element)
{
xmlBufferPtr buffer;
char *xml_string;

// Copy element to ensure XML namespaces are included
element_t element_copy;
element_copy = xmlCopyNode (element, 1);

buffer = xmlBufferCreate ();
xmlNodeDump (buffer, element_copy->doc, element_copy, 0, 0);
xmlFreeNode (element_copy);

xml_string = g_strdup ((char *) xmlBufferContent (buffer));

xmlBufferFree (buffer);
return xml_string;
}
3 changes: 3 additions & 0 deletions util/xmlutils.h
Expand Up @@ -186,4 +186,7 @@ element_t element_first_child (element_t);

element_t element_next (element_t);

gchar *
element_to_string (element_t element);

#endif /* not _GVM_XMLUTILS_H */

0 comments on commit ba1b52a

Please sign in to comment.