* xmlNodeGetContent: * @cur: the node being read * * 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. * Returns a new #xmlChar * or NULL if no content is available. * It's up to the caller to free the
| 5655 | * It's up to the caller to free the memory with xmlFree(). |
| 5656 | */ |
| 5657 | xmlChar * |
| 5658 | xmlNodeGetContent(const xmlNode *cur) |
| 5659 | { |
| 5660 | xmlBufPtr buf; |
| 5661 | xmlChar *ret; |
| 5662 | |
| 5663 | if (cur == NULL) |
| 5664 | return (NULL); |
| 5665 | |
| 5666 | switch (cur->type) { |
| 5667 | case XML_DOCUMENT_NODE: |
| 5668 | case XML_HTML_DOCUMENT_NODE: |
| 5669 | case XML_ENTITY_REF_NODE: |
| 5670 | break; |
| 5671 | |
| 5672 | case XML_DOCUMENT_FRAG_NODE: |
| 5673 | case XML_ELEMENT_NODE: |
| 5674 | case XML_ATTRIBUTE_NODE: |
| 5675 | case XML_ENTITY_DECL: { |
| 5676 | xmlNodePtr children = cur->children; |
| 5677 | |
| 5678 | if (children == NULL) |
| 5679 | return(xmlStrdup(BAD_CAST "")); |
| 5680 | |
| 5681 | /* Optimization for single text children */ |
| 5682 | if (((children->type == XML_TEXT_NODE) || |
| 5683 | (children->type == XML_CDATA_SECTION_NODE)) && |
| 5684 | (children->next == NULL)) { |
| 5685 | if (children->content == NULL) |
| 5686 | return(xmlStrdup(BAD_CAST "")); |
| 5687 | return(xmlStrdup(children->content)); |
| 5688 | } |
| 5689 | |
| 5690 | break; |
| 5691 | } |
| 5692 | |
| 5693 | case XML_CDATA_SECTION_NODE: |
| 5694 | case XML_TEXT_NODE: |
| 5695 | case XML_COMMENT_NODE: |
| 5696 | case XML_PI_NODE: |
| 5697 | if (cur->content != NULL) |
| 5698 | return(xmlStrdup(cur->content)); |
| 5699 | else |
| 5700 | return(xmlStrdup(BAD_CAST "")); |
| 5701 | |
| 5702 | case XML_NAMESPACE_DECL: |
| 5703 | return(xmlStrdup(((xmlNsPtr) cur)->href)); |
| 5704 | |
| 5705 | default: |
| 5706 | return(NULL); |
| 5707 | } |
| 5708 | |
| 5709 | buf = xmlBufCreateSize(64); |
| 5710 | if (buf == NULL) |
| 5711 | return (NULL); |
| 5712 | xmlBufSetAllocationScheme(buf, XML_BUFFER_ALLOC_DOUBLEIT); |
| 5713 | xmlBufGetNodeContent(buf, cur); |
| 5714 | ret = xmlBufDetach(buf); |