Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can xmlNodeGetContent() return NULL?

Tags:

c

xml

libxml2

libxml2 has function xmlNodeGetContent(), which is documented as follows in http://xmlsoft.org/html/libxml-tree.html#xmlNodeGetContent:

xmlChar *xmlNodeGetContent  (const xmlNode * cur)

Read the value of a node, this can be either the text carried directly by this node if it's a TEXT node or the aggregate string of the values carried by this node child's (TEXT and ENTITY_REF). Entity references are substituted.

cur: the node being read Returns: a new #xmlChar * or NULL if no content is available. It's up to the caller to free the memory with xmlFree().

If cur is an empty node, for example <foo/> (or the equivalent <foo></foo>), then xmlNodeGetContent() returns an empty string, not NULL, and that is fine IMO.

But then, in which case would xmlNodeGetContent() return NULL? What would be a minimalistic XML example where that would happen?

like image 420
user1387866 Avatar asked Aug 14 '26 11:08

user1387866


1 Answers

If the node passed to xmlNodeGetContent is NULL the return value will be NULL:

xmlNode *current_node = NULL;
xmlChar *ret_val;

ret_val = xmlNodeGetContent(current_node);

if(ret_val == NULL)
    printf("ret_val is NULL\n");

If the node is a Valid xmlNode, the function could return NULL in different cases, you can check the function in tree.c:

xmlChar *xmlNodeGetContent(const xmlNode *cur)
{
if(cur == NULL)
    return (NULL);
switch(cur->type)
    {
    case XML_DOCUMENT_FRAG_NODE:
    case XML_ELEMENT_NODE:
        {
        xmlBufPtr buf;
        xmlChar *ret;

        buf = xmlBufCreateSize(64);
        if(buf == NULL)
            return (NULL);
        xmlBufGetNodeContent(buf, cur);
        ret = xmlBufDetach(buf);
        xmlBufFree(buf);
        return (ret);
        }
    case XML_ATTRIBUTE_NODE:
        return (xmlGetPropNodeValueInternal((xmlAttrPtr)cur));
    case XML_COMMENT_NODE:
    case XML_PI_NODE:
        if(cur->content != NULL)
            return (xmlStrdup(cur->content));
        return (NULL);
    case XML_ENTITY_REF_NODE:
        {
        xmlEntityPtr ent;
        xmlBufPtr buf;
        xmlChar *ret;

        /* lookup entity declaration */
        ent = xmlGetDocEntity(cur->doc, cur->name);
        if(ent == NULL)
            return (NULL);

        buf = xmlBufCreate();
        if(buf == NULL)
            return (NULL);

        xmlBufGetNodeContent(buf, cur);

        ret = xmlBufDetach(buf);
        xmlBufFree(buf);
        return (ret);
        }
    case XML_ENTITY_NODE:
    case XML_DOCUMENT_TYPE_NODE:
    case XML_NOTATION_NODE:
    case XML_DTD_NODE:
    case XML_XINCLUDE_START:
    case XML_XINCLUDE_END:
        return (NULL);
    case XML_DOCUMENT_NODE:
    #ifdef LIBXML_DOCB_ENABLED
    case XML_DOCB_DOCUMENT_NODE:
    #endif
    case XML_HTML_DOCUMENT_NODE:
        {
        xmlBufPtr buf;
        xmlChar *ret;

        buf = xmlBufCreate();
        if(buf == NULL)
            return (NULL);

        xmlBufGetNodeContent(buf, (xmlNodePtr)cur);

        ret = xmlBufDetach(buf);
        xmlBufFree(buf);
        return (ret);
        }
    case XML_NAMESPACE_DECL:
        {
        xmlChar *tmp;

        tmp = xmlStrdup(((xmlNsPtr)cur)->href);
        return (tmp);
        }
    case XML_ELEMENT_DECL:
        /* TODO !!! */
        return (NULL);
    case XML_ATTRIBUTE_DECL:
        /* TODO !!! */
        return (NULL);
    case XML_ENTITY_DECL:
        /* TODO !!! */
        return (NULL);
    case XML_CDATA_SECTION_NODE:
    case XML_TEXT_NODE:
        if(cur->content != NULL)
            return (xmlStrdup(cur->content));
        return (NULL);
    }
return (NULL);
}
like image 108
Fabio_MO Avatar answered Aug 16 '26 02:08

Fabio_MO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!