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?
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.