简体   繁体   中英

In java, how do I create my own xml subclassed elements in a DOM returned by 3rd party tools

So I'm using HTMLCleaner which returns a org.w3c.dom compatible DOM. Now, I want to insert my own subclassed Elements (which implement additional functionality outside the dom) into this dom tree. Is this possible?

If you use the Document.createElement(), you can't say that you want it to create your implementation of an element. Ideally I don't want my class to implement Element and Node have to manually do all the work that has already been done. I thought that I could subclass ElementImpl from Xerces and potentially add it to the DOM I have. However Xerces expects to be created into DOMs that implement CoreDocumentImpl etc.

It would be a pity, from an elegance point of view. If I had to use plain Element and manage to identify them by their attributes/names etc, as each one needs to point to particular positions in an index.

DOM is not the prettiest API to work with. If you want simplicity, use DOM4J : - Wrap the DOM document into a DOM4J document - Use DOM4J instead of DOM to build the existing document or wrap it in a DOM4J document

If possible, I'd recommend ditching Xerces for JAXB. It's a much friendlier API.

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