问题
I have a jTextPane that has HTMLEditorKit set as the default editorKit. Calling getDocument on the jTextPane returns an HTMLDocument. I can traverse through this Document, navigating around the elements, but I can't seem to find a way to actually get the text contents of individual elements. I know in the DOM, there is a getTextContents() on each element, but I'm not working with the W3C DOM.
回答1:
As you traverse the document's ElementIterator
, shown here, use the getStartOffset()
and getEndOffset()
of each Element
to calculate the parameters needed to call getText()
.
来源:https://stackoverflow.com/questions/9559997/get-the-text-content-of-individual-elements-from-an-htmldocument