如何使用 Jericho HTML Parser 解析 XML [英] How to parse XML using Jericho HTML Parser

查看:40
本文介绍了如何使用 Jericho HTML Parser 解析 XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Java 和 servlet 的新手,目前正在尝试使用 Jericho XML Parser 解析 XML.例如,我想从每个链接标签中获取链接,但它没有显示任何内容,并且总数为 27(只能获得正确的总数而不带字符串).有知道的请教教我.

I'm new to java and servlet and currently trying to parse XML using Jericho XML Parser. For instance, i want to get links from each link tag, but it dose not show anything,and total number says 27(can get only correct total number without string). Anyone who knows how to, please teach me.

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.Source;

@WebServlet(urlPatterns = { "/HelloServlet"})

public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException,MalformedURLException{

    resp.setContentType("text/html; charset=UTF-8");
    PrintWriter out = resp.getWriter();
    out.println("<html>");
    out.println("<head><meta http-equiv='content-type' content='text/html; charset=UTF-8'></head>");
    out.println("<body>");
    Source source = new Source(new URL("http://news.yahoo.com/rss/"));
    source.fullSequentialParse();


    List<Element> Linklist = source.getAllElements("link");


    if(Linklist!=null){
        out.println("<p>total:"+Linklist.size()+"</p>");
        for(Element link: Linklist){
            out.println("<p>"+link.getContent().toString()+"</p>");
        }
    }


    out.println("</body>");
    out.println("</html>");
}


}

推荐答案

根据 Jericho HTML Parser 主页 Jericho 用于操作 HTML 文档.但是来自 Yahoo 的 RSS 是 XML,您可以使用 Java 的标准 XML 来解析此文档并提取链接标签.下面是一个例子:

According to the Jericho HTML Parser homepage Jericho is for manipulating HTML documents. But the RSS from Yahoo is XML and you can use Java's standard XML to parse this document and to extract the link tags. Here is an example:

import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;
import java.util.List;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

// ...

private List<String> getRssLinks() throws ParserConfigurationException,
    SAXException, IOException 
{
  final List<String> rssLinks = new LinkedList<String>();
  final URL url = new URL("http://news.yahoo.com/rss/");
  final Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                       .parse(url.openStream());
  final NodeList linkNodes = doc.getElementsByTagName("link");
  for(int i = 0; i < linkNodes.getLength(); i++) {
    final Element linkElement = (Element) linkNodes.item(i);
    rssLinks.add(linkElement.getTextContent());
  }

  return rssLinks;
}

这篇关于如何使用 Jericho HTML Parser 解析 XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆