简体   繁体   中英

find by org.w3c.dom.Element object as parameter on org.dom4j.Document (convert org.w3c.dom.Element to org.dom4j.Element)

I have a org.w3c.dom.Document parsed by org.dom4j.io.DOMReader .

I want to search the dom4j DOM document via org.w3c.dom.Element .

say, I have a org.w3c.dom.Element that I need to locate on the dom4j document. I don't have any element information except for having the org.w3c.dom.Element object as a parameter.

I want something like dom4j doc.findByDomElement(org.w3c.dom.Element element) which would return a org.dom4j.Element .

What you need isn't provided by dom4j out of the box. Dom4j allows you to parse an org.w3c.dom.Document as an org.dom4j.Document , but then you cannot search through the dom4j Document by an org.w3c.dom.Element ; you should do that on your own method. For instance, you can use xpath to search some nodes. Also, dom4j provides org.dom4j.dom.DOMNodeHelper class which is a collection of utility methods to do some conversion from org.w3c.dom objects to org.dom4j objects but I've not found the method you need. Take a look here . In addition, dom4j provides the org.dom4j.io.DOMWriter class to do the opposite of org.dom4j.io.DOMReader .

When you say:

I don't have any element information except for having the org.w3c.dom.Element object as a parameter.

Well, I think the Element object contains all the informations that you need to search manually through the dom4j tree.

Finally, I'd like to suggest you to use only one library to handle xml in your code. Do you have any particular needs? Why are you using both org.dom4j.Document and org.w3c.dom.Document ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM