小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

使用javax.swing.text.html包解析HTML文檔

 素行 2007-04-19
 下面是一個(gè)例子:  
  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對(duì)象  
        */  
      org.w3c.dom.Document   newDoc;  
      /**  
        *   根結(jié)點(diǎn)對(duì)象  
        */  
      private   org.w3c.dom.Element   root;  
   
      /**  
        *   是否忽略<img>標(biāo)簽  
        */  
      private   boolean   ignoreImg=true;  
   
      /**  
        *   設(shè)置是否忽略按鈕標(biāo)簽  
        */  
      private   boolean   ignoreButton=true;  
   
      /**  
        *   文件路徑  
        */  
      private   String   filePath;  
   
      /**  
        *   是否調(diào)用了函數(shù)parse()  
        */  
      private   boolean   isParse=false;  
   
      /**  
        *   處理結(jié)果可接受的html   tagname,如果不在這個(gè)表里面則忽略掉這個(gè)元素  
        */  
      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);  
      }  
   
      /**  
        *   開(kāi)始解析  
        */  
      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;  
   
      }  
   
      /**  
        *   返回創(chuàng)建好的Document對(duì)象  
        *   @return  
        */  
      public   org.w3c.dom.Document   getXmlDocument(){  
          if(isParse==false){  
              parse();  
              isParse=true;  
          }  
          return   this.newDoc;  
      }  
   
      /**  
        *   返回標(biāo)準(zhǔn)的xml字符串  
        *   @return  
        */  
      public   String   getXmlStr(){  
          if(isParse==false){  
              parse();  
              isParse=true;  
          }  
          return   XmlParser.elementToString(root);  
      }  
   
      /**  
        *   設(shè)置是否忽略img標(biāo)簽  
        *   @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;  
      }  
   
      /**  
        *   設(shè)置是否忽略button標(biāo)簽  
        *   @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);  
   
          }  
      }  
   
  }

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多