用正则表达式,参见java.util.regex包

解决方案 »

  1.   

    楼主,我也在被这个问题困扰ing国外有个相关的API,但没有好的实例,我下载了都不知道如何用。如果用正则运算式的话很繁琐,我测试就测试了n年狂晕
      

  2.   

    就是忽略html代码 显示文本信息的样子
    我下载了htmlparser 但是不知道怎么用 请问有什么例子吗?
      

  3.   

    用JTidy开源包,他提供了DOM 和SAX接口来解析html数据。
      

  4.   

    NodeList list=parser.parse(NodeFilter);
    里面的参数怎么办呢
      

  5.   

    用javax.swing.text.html包
    下面是一个例子给你看看:
    import javax.swing.text.html.HTMLEditorKit$ParserCallback;
    import javax.swing.text.html.*;
    import javax.swing.text.*;
    import com.pdcss.debug.*;
    import java.util.*;
    import javax.swing.text.html.parser.*;
    import java.io.*;
    import java.net.*;
    import com.pdcss.xml.*;
    import com.pdcss.debug.DebugOut;
    public class HTMLToXML {
      /**
       * document对象
       */
      org.w3c.dom.Document newDoc;
      /**
       * 根结点对象
       */
      private org.w3c.dom.Element root;  /**
       * 是否忽略<img>标签
       */
      private boolean ignoreImg=true;  /**
       * 设置是否忽略按钮标签
       */
      private boolean ignoreButton=true;  /**
       * 文件路径
       */
      private String filePath;  /**
       * 是否调用了函数parse()
       */
      private boolean isParse=false;  /**
       * 处理结果可接受的html tagname,如果不在这个表里面则忽略掉这个元素
       */
      private ArrayList acceptedTags;  public HTMLToXML(String filePath) {
        this.filePath=filePath;
        newDoc = XmlParser.buildNewDocument();
        root= newDoc.createElement("HTMLToXML");
        newDoc.appendChild(root);    //增加可接受的元素的tag
        acceptedTags=new ArrayList(5);
        acceptedTags.add(HTML.Tag.INPUT);
        acceptedTags.add(HTML.Tag.TEXTAREA);
        acceptedTags.add(HTML.Tag.SELECT);
      }  /**
       * 开始解析
       */
      private void parse(){
        HTMLEditorKit.Parser parser = new ParserDelegator();
        HTMLEditorKit.ParserCallback callback = new MyParserCallback();
        try {
          URL u = new File(filePath).toURL();
          InputStream in = u.openStream();
          InputStreamReader reader = new InputStreamReader(in);
          parser.parse(reader, callback, false);
          reader.close();
        }
        catch (IOException e) {
        }
        isParse=true;  }  /**
       * 返回创建好的Document对象
       * @return
       */
      public org.w3c.dom.Document getXmlDocument(){
        if(isParse==false){
          parse();
          isParse=true;
        }
        return this.newDoc;
      }  /**
       * 返回标准的xml字符串
       * @return
       */
      public String getXmlStr(){
        if(isParse==false){
          parse();
          isParse=true;
        }
        return XmlParser.elementToString(root);
      }  /**
       * 设置是否忽略img标签
       * @param isIgnore
       */
      public void setIgnoreImg(boolean isIgnore){
        ignoreImg=isIgnore;
        if(ignoreImg==true){
          acceptedTags.remove(HTML.Tag.IMG);
        }else{
          if(!acceptedTags.contains(HTML.Tag.IMG)){
            acceptedTags.add(HTML.Tag.IMG);
          }
        }
      }  public boolean getIgnoreImg(){
        return ignoreImg;
      }  /**
       * 设置是否忽略button标签
       * @param isIgnore
       */
      public void setIgnoreButton(boolean isIgnore){
        ignoreButton=isIgnore;
      }
      private class MyParserCallback extends HTMLEditorKit.ParserCallback {
       public void handleStartTag(HTML.Tag tag, MutableAttributeSet attributes,   int position)  {
         handleTag(tag, attributes, position);
        }    public void handleSimpleTag(HTML.Tag tag, MutableAttributeSet attributes, int position) {
          handleTag(tag, attributes, position);
        }    private void handleTag(HTML.Tag tag, MutableAttributeSet attributes, int position){
          if(!acceptedTags.contains(tag)){
            return;
          }
          if(ignoreButton==true){
            if (tag == HTML.Tag.INPUT) {
              String type = attributes.getAttribute(HTML.Attribute.TYPE).toString();
              //不是按钮
              if (type != null){
                if (type.equalsIgnoreCase("button") || type.equalsIgnoreCase("submit") || type.equalsIgnoreCase("reset")) {
                  return;
                }
              }
            }
          }
          org.w3c.dom.Element newElement = newDoc.createElement(tag.toString());
          Enumeration enum = attributes.getAttributeNames();
          Object attributeName;
          while (enum.hasMoreElements()) {
            attributeName = enum.nextElement();
            newElement.setAttribute(attributeName.toString(), attributes.getAttribute(attributeName).toString());
          }
          root.appendChild(newElement);    }
      }}
      

  6.   

    非常感谢fmzhangfm :)
    可是偶要用的是htmlparser拉 
    8是这个