Unless you have a valid xhtml that reminds me of a reminder, you should first apply jtidy to tidy up the html and make it valid xhtml.
See this , for example jtidy code.
html DOM.
:
final DocumentBuilderFactory newFactory = DocumentBuilderFactory.newInstance();
final DocumentBuilder documentBuilder = newFactory.newDocumentBuilder();
Document document = documentBuilder.parse( new InputSource( new StringReader( string ) ) );
.