开发者

How to parse html content in xml using tagsoup in android

开发者 https://www.devze.com 2023-04-04 05:55 出处:网络
Can anybody tell me how to parse HTML content as XML using TagSoup within开发者_如何学编程 Android? I am looking for functional code examples if possible.XMLReader xmlReader = XMLReaderFactory.createX

Can anybody tell me how to parse HTML content as XML using TagSoup within开发者_如何学编程 Android? I am looking for functional code examples if possible.


XMLReader xmlReader = XMLReaderFactory.createXMLReader ("org.ccil.cowan.tagsoup.Parser");
ContentHandler handler = new DefaultHandler () {
  public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException
  {
    // ...
  }
};
xmlReader.setContentHandler (handler);
xmlReader.parse (new InputSource (input));


Below is code which should provide you with a means of parsing the web page via the Document produced by 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());

        // Parse the document as needed
        Node node = doc.query("...");
    }
    catch(IOException e)
    { ... }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号