开发者

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

开发者 https://www.devze.com 2023-02-21 21:50 出处:网络
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.

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 l开发者_如何学Pythonocate 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?

0

精彩评论

暂无评论...
验证码 换一张
取 消