Append HTML to XOM element

132 views Asked by At

I need to prepend HTML to existed XOM element. HTML represented as a String.

Here are my actions

  1. Parse String into XOM Document
  2. Remove root element(X) from Document
  3. Prepend X to desired target element (Y)

I get an exception

nu.xom.WellformednessException: Cannot remove the root element
    at nu.xom.Document.removeChild(Unknown Source)
    at nu.xom.Node.detach(Unknown Source)

X element HTML

<div>
  Some test text
</div>
1

There are 1 answers

0
aleks.n.fedorov On

Current XOM implementation does not allow to remove root node.

To achieve desired goal it is need to add fake root and then detach required node.

In a context of mentioned step set:

Replace step 2 with next

  1. Add fake root element - document.setRootElement(new Element("div"))
  2. Detach desired element - element.detach()