gpt4 book ai didi

xmlNodeGetContent() 可以返回 NULL 吗?

转载 作者:太空宇宙 更新时间:2023-11-04 02:27:33 26 4
gpt4 key购买 nike

libxml2 具有函数 xmlNodeGetContent() ,在 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().

如果cur是一个空节点,例如<foo/> (或等效的 <foo></foo> ),然后是 xmlNodeGetContent()返回一个空字符串,而不是 NULL ,这在我看来很好。

但是,在这种情况下 xmlNodeGetContent()返回 NULL ?什么是会发生这种情况的极简 XML 示例?

最佳答案

如果传递给 xmlNodeGetContent 的节点为 NULL,则返回值为 NULL:

xmlNode *current_node = NULL;
xmlChar *ret_val;

ret_val = xmlNodeGetContent(current_node);

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

如果节点是一个有效的xmlNode,函数在不同情况下可能会返回NULL,你可以在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);
}

关于xmlNodeGetContent() 可以返回 NULL 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48901026/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com