Skip to content Skip to sidebar Skip to footer

How Can I Parse A Html String In Java?

Given the string '
Hello World!
', what is the (easiest) way to get a DOM Element representing it?

Solution 1:

If you have a string which contains HTML you can use Jsoup library like this to get HTML elements:

String htmlTable= "<table><tr><td>Hello World!</td></tr></table>";
Documentdoc= Jsoup.parse(htmlTable);

// then use something like this to get your element:Elementstds= doc.getElementsByTag("td");

// tds will contain this one element: <td>Hello World!</td>

Good luck!

Solution 2:

Here's a way:

import java.io.*;
import javax.swing.text.*;
import javax.swing.text.html.*;
import javax.swing.text.html.parser.*;

publicclassHtmlParseDemo {
   publicstaticvoidmain(String [] args)throws Exception {
       Readerreader=newStringReader("<table><tr><td>Hello</td><td>World!</td></tr></table>");
       HTMLEditorKit.Parserparser=newParserDelegator();
       parser.parse(reader, newHTMLTableParser(), true);
       reader.close();
   }
}

classHTMLTableParserextendsHTMLEditorKit.ParserCallback {

    privatebooleanencounteredATableRow=false;

    publicvoidhandleText(char[] data, int pos) {
        if(encounteredATableRow) System.out.println(newString(data));
    }

    publicvoidhandleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
        if(t == HTML.Tag.TR) encounteredATableRow = true;
    }

    publicvoidhandleEndTag(HTML.Tag t, int pos) {
        if(t == HTML.Tag.TR) encounteredATableRow = false;
    }
}

Solution 3:

you could use HTML Parser, which a Java library used to parse HTML in either a linear or nested fashion. It is an open source tool and can be found on SourceForge

Solution 4:

You could use Swing:

How do you make use of the HTML-processing capabilities that are built into Java? You may not know that Swing contains all the classes necessary to parse HTML. Jeff Heaton shows you how.

Solution 5:

I've used Jericho HTML Parser it's OSS, detects(forgives) badly formatted tags and is lightweight

Post a Comment for "How Can I Parse A Html String In Java?"