HTML page for XHTML using TagSoup

Sorry if this is too easy, but I just couldn't find the tutorial and documentation of the Java version of TagSoup.

Basically, I want to download an HTML web page from the Internet and turn it into XHTML contained in a string. How to do it with TagSoup?

Thanks!

+3
source share
2 answers

Something like that:

wget -O - example.com/bad.html | java -jar tagsoup.jar

Or, from Java:

HTML parsing:

  • Create instance org.ccil.cowan.tagsoup.Parser
  • Provide your own SAX2 ContentHandler
  • Provide InputSourceHTML Link
  • And parse()!
+7
source

Below is the code that should give you the opportunity to display a web page and analyze it using TagSoup ...

        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet("http://streak.espn.go.com/en/?date=20120824");
        HttpResponse response = client.execute(request);

        // Check if server response is valid
        StatusLine status = response.getStatusLine();
        if (status.getStatusCode() != 200) {
            throw new IOException("Invalid response from server: " + status.toString());
        }

        // Pull content stream from response
        HttpEntity entity = response.getEntity();
        InputStream inputStream = entity.getContent();

        try
        {
            XMLReader parser = XMLReaderFactory.createXMLReader("org.ccil.cowan.tagsoup.Parser");

            // Use the TagSoup parser to build an XOM document from HTML
            Document doc = new Builder(parser).build(builder.toString());

            // Push your data to string or XML
            doc.toString();
            doc.toXML();
        }
        catch(IOException e)
        { ... }
+1

Source: https://habr.com/ru/post/1720469/


All Articles