JAVA利用SAX如何解析html提取里面的超链接,不知道改怎么提取,和<a href></a>标签有关吗?

解决方案 »

  1.   

    for(Element e : Jsoup.parse(url).select("a")){
        System.out.println(e.attr("href"));
    }
      

  2.   


    jsoup.jar 比 htmlparser更好。
      

  3.   

    sax就是要判断是什么标签,获取什么内容public class StrutsXMLParser {
       Map<String, Object> map = new HashMap<String, Object>();   class StrutsXMLHandler extends SAXHandler {
          private String cls;
          private boolean isUrl;
          private List<String> fileList = new ArrayList<String>();      @Override
          public void startDocument() throws SAXException {
          }      @Override
          public void startElement(String namespaceURI, String localName,
                String qName, Attributes atts) throws SAXException {
             if("include".equals(qName)) {
                fileList.add(atts.getValue("file"));
             }         if("constant".equals(qName)) {
                map.put(atts.getValue("name"), atts.getValue("value"));
             }         if("action".equals(qName)) {
                cls = atts.getValue("class");
             }         if("result".equals(qName) && "error".equals(atts.getValue("name"))) {
                isUrl = true;
             }
          }      @Override
          public void characters(char[] ch, int start, int length) throws SAXException {
             if(cls != null && isUrl) {
                String url = new String(ch, start, length);            if(!map.containsKey(cls)) {
                   cls = cls.substring(cls.lastIndexOf(".") + 1);
                   map.put(cls, url);
                }            isUrl = false;
                cls = null;
             }
          }      @Override
          public void endElement(String namespaceURI, String localName, String qName)
                throws SAXException {
          }      @SuppressWarnings("unchecked")
          @Override
          public void endDocument() throws SAXException {
             if(fileList.size() > 0) {
                map.put("fileList", fileList);
             }
          }
       }   @SuppressWarnings("unchecked")
       public Map<String, Object> parser(String xml) throws Exception {
          String path = xml.substring(0, xml.lastIndexOf("/") + 1);
          SAXParserFactory spf = SAXParserFactory.newInstance();
          SAXParser sp = spf.newSAXParser();
          XMLReader xr = sp.getXMLReader();
          StrutsXMLHandler handler = new StrutsXMLHandler();
          xr.setContentHandler(handler);
          xr.parse(xml);      List<String> fileList = (List<String>) map.get("fileList");      if(fileList != null) {
             map.remove("fileList");
             Iterator it = fileList.iterator();         while(it.hasNext()) {
                parser(path + it.next());
             }
          }      return map;
       }
      

  4.   


    <constant name="devMode" value="true" />
    <constant name="struts.multipart.saveDir" value="c:\HDM_TOOL\upload\"></constant>
    <constant name="user.data.dir" value="c:\HDM_TOOL\DATA\"></constant>