Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3238938
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:54:41+00:00 2026-05-17T17:54:41+00:00

I’m using libxml2 to parse HTML. The HTML might look like this: <div> Some

  • 0

I’m using libxml2 to parse HTML. The HTML might look like this:

<div>
    Some very very long text here.
</div>

I want to insert a child node, e.g. a header, in before the text, like this:

<div>
    <h3>
        Some header here
    </h3>
    Some very very long text here.
</div>

Unfortunately, libxml2 always adds my header after the text, like this:

<div>
    Some very very long text here.
    <h3>
        Some header here
    </h3>
</div>

How can I solve this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:54:42+00:00Added an answer on May 17, 2026 at 5:54 pm

    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.

    xmlInitParser();
    
    // Create an XML document
    std::string content( "<html><head/><body><div>Some long text here</div></body></html>" );
    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->nodesetval;
    xmlNodePtr divNode = nodes->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->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->next;
        }
    
    // Display the result
    xmlDocDump( stdout, doc );
    
    xmlCleanupParser();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have thousands of HTML files to process using Groovy/Java and I need to
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.