如何在android中使用tagoup解析xml中的html内容

发布于 2024-12-04 07:02:22 字数 72 浏览 0 评论 0原文

谁能告诉我如何在 Android 中使用 TagSoup 将 HTML 内容解析为 XML?如果可能的话,我正在寻找功能代码示例。

Can anybody tell me how to parse HTML content as XML using TagSoup within Android? I am looking for functional code examples if possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅气尐潴 2024-12-11 07:02:22
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));
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));
心碎的声音 2024-12-11 07:02:22

下面的代码应该为您提供一种通过 TagSoup 生成的 Document 解析网页的方法。

    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)
    { ... }

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)
    { ... }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文