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 6655603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:32:24+00:00 2026-05-26T01:32:24+00:00

I have a org.dom4j.Document instance that is a DefaultDocument implementation to be specific. I

  • 0

I have a org.dom4j.Document instance that is a DefaultDocument implementation to be specific. I would like to insert a new node just before an other one. I do not really understand the dom4j api, I am confused of the differences between Element and DOMElement and stuff.

org.dom4j.dom.DOMElement.insertBefore is not working for me because the Node I have is not a DOMElement. DOMNodeHelper.insertBefore is nor good because I have org.dom4j.Node instances and not org.w3c.dom.Node instances. OMG.

Could you give me a little code snippet that does this job for me?

This is what I have now:

// puts lr's to the very end in the xml, but I'd like to put them before 'e'
for(Element lr : loopResult) {
  e.getParent().add(lr);
}
  • 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-26T01:32:24+00:00Added an answer on May 26, 2026 at 1:32 am

    It’s an “old” question, but the answer may still be relevant. One problem with the DOM4J API is that there are too many ways to do the same thing; too many convenience methods with the effect that you cannot see the forest for the trees. In your case, you should get a List of child elements and insert your element at the desired position: Something like this (untested):

    // get a list of e's sibling elements, including e
    List elements = e.getParent().elements();
    // insert new element at e' position, i.e. before e
    elements.add(elements.indexOf(e), lr);
    

    Lists in DOM4J are live lists, i.e. a mutating list operation affects the document tree and vice versa

    As a side note, DOMElement and all the other classes in org.dom4j.dom is a DOM4J implementation that also supports the w3c DOM API. This is rarely needed (I would not have put it and a bunch of the other “esoteric” packges like bean, datatype, jaxb, swing etc, in the same distribution unit). Concentrate on the core org.dom4j, org.dom4j.tree, org.dom4j.io and org.dom4j.xpathpackages.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a org.w3c.dom.Node object. I would like to see if it has any
I have seen the official demos on lwjgl.org but I would like to see
I have a XML org.w3c.dom.Document object. It looks sorta like this: <A> <B> <C/>
I just wanted some opinions from people that have run Selenium ( http://selenium.openqa.org )
I have a org.w3c.dom.Document parsed by org.dom4j.io.DOMReader . I want to search the dom4j
I have an org-mode task list that I keep in version control. I would
this is my situation: I have two org.w3c.dom.Document created from two xml files. What
Let's say we have an org.w3c.dom.Document representing the following XML: <Fruits> <Apples> <Red/> <Green/>
Am I right thinking that Tigris.org have stopped maintaining their SVN binary package for
I have some links at the page like: 123test.com/456 abc_test.com/sdfsd abc_test.org I have to

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.