Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The text content is a child node, so you can get a pointer to the text node and use the xmlAddPrevSibling function to add the element. Here is an example, but without error handling or proper cleanup.</p> <pre><code>xmlInitParser(); // Create an XML document std::string content( "&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;div&gt;Some long text here&lt;/div&gt;&lt;/body&gt;&lt;/html&gt;" ); xmlDocPtr doc = xmlReadMemory( content.c_str(), content.size(), "noname.xml", 0, 0 ); // Query the XML document with XPATH, we could use the XPATH text() function // to get the text node directly but for the sake of the example we'll get the // parent 'div' node and iterate its child nodes instead. std::string xpathExpr( "/html/body/div" ); xmlXPathContextPtr xpathCtx = xmlXPathNewContext( doc ); xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression( BAD_CAST xpathExpr.c_str(), xpathCtx ); // Get the div node xmlNodeSetPtr nodes = xpathObj-&gt;nodesetval; xmlNodePtr divNode = nodes-&gt;nodeTab[ 0 ]; // Iterate the div child nodes, though in this example we know // there'll only be one node, the text node. xmlNodePtr divChildNode = divNode-&gt;xmlChildrenNode; while( divChildNode != 0 ) { if( xmlNodeIsText( divChildNode ) ) { // Create a new element with text node xmlNodePtr headingNode = xmlNewNode( 0, BAD_CAST "h3" ); xmlNodePtr headingChildNode = xmlNewText( BAD_CAST "Some heading here" ); xmlAddChild( headingNode, headingChildNode ); // Add the new element to the existing tree before the text content xmlAddPrevSibling( divChildNode, headingNode ); break; } divChildNode = divChildNode-&gt;next; } // Display the result xmlDocDump( stdout, doc ); xmlCleanupParser(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload