title: Dom4j detach node, Jython
body: <p>I am using Dom4j to detach a node, like below:</p>
  
  <pre><code>&lt;div name="divName"&gt;
      Some Text Here
      &lt;span&gt;Some Text Here&lt;/span&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I am selecting the div node by name and then using the detach method to remove it:</p>
  
  <pre><code>xpathValue = "//*[contains(@name, 'divName')]"
  xpath = dom.createXPath(xpathValue)
      if xpath != None:
          nodes = xpath.selectNodes(dom)
          if len(nodes) &gt; 0:
              for node in nodes:
                  node.detach()
  </code></pre>
  
  <p>This seems to remove the div fine, I noticed that it also removes elements and text within that div also.  What I am looking to achive is removing the div without removing the elements and text inside the div, resulting in this:</p>
  
  <pre><code>Some Text Here
  &lt;span&gt;Some Text Here&lt;/span&gt;
  </code></pre>
  
  <p>Is it possible to achive this with dom4j?  If not any suggestions on how to go about this?</p>
  
  <p>Cheers</p>
  
  <p>Eef</p>
  
  <p><strong>Update:</strong></p>
  
  <p>@alamar</p>
  
  <p>I have achived what I wanted by taking your code and editing it a little and this is what I have come up with:</p>
  
  <pre><code>   xpathValue = "//*[contains(@name, 'divName')]"
     xpath = dom.createXPath(xpathValue)
      if xpath != None:
          nodes = xpath.selectNodes(dom)
          if len(nodes) &gt; 0:
              for node in nodes:
                  parent = node.getParent()
                  nodeContents = node.content()
                      if len(nodeContents) &gt; 0:
                          for subNode in nodeContents:
                              parent.add(subNode.clone().detach())
              node.detach()
  </code></pre>
  
  <p>This seems to work, but adds the nodes to the end of the parent node in the below situation:</p>
  
  <pre><code>&lt;div name="parent"&gt;
      &lt;div name="divName"&gt;
          Some Text Here
          &lt;span&gt;Some Text Here&lt;/span&gt;
      &lt;/div&gt;
      &lt;div name="keep"&gt;&lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>The result is this:</p>
  
  <pre><code>&lt;div name="parent"&gt;
      &lt;div name="keep"&gt;&lt;/div&gt;
      Some Text Here
      &lt;span&gt;Some Text Here&lt;/span&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>I am trying to figure out how to get the contents of the removed node to stay in its original position, before thed div named "keep", instead of being added after the div with the name "keep".  I have tried a few thing but can not seem achive this, could anyone help?</p>
  
  <p>Eef</p>