htmlParser解析html文件

java 代码
  1. public class HtmlFileIo  {   
  2.        
  3.     public static void main (String[] args)   
  4.     {   
  5.         String strFile = "file://tenwa-98bf4155e/zhanghftemp/office/JMO_34.htm";   
  6.         String strDir = "file://tenwa-98bf4155e/zhanghftemp/office";   
  7.         try  
  8.         {   
  9.             //读取一个文件的内容   
  10.             //readByHtml(strFile);   
  11.             //得到一个文件最后修改时间   
  12.             //getRemoteLastModified(strFile);   
  13.             ArrayList al = getRemoteDirInfo(strDir);   
  14.             //获取一个文件夹下所有文件的名称   
  15.             for(int i=0;i<al.size();i++){   
  16.                 System.out.println(al.get(i));   
  17.                 String strName = (al.get(i)).toString();   
  18.                 //获取楼层数   
  19.                 System.out.println(strName.substring(0, strName.indexOf('.')).split("_")[1]);   
  20. //              System.out.println(strDir+"/"+al.get(i));   
  21.                 //获取楼层中的单元信息   
  22. //              ArrayList alArea = readByHtml(strDir+"/"+al.get(i));   
  23. //              for(int j=0;j<alArea.size();j++){   
  24. //                  HashMap hm = (HashMap)alArea.get(j);   
  25.                     //获取楼层中的单元信息   
  26. //                  System.out.println("href:"+hm.get("href")+"      shape:"+hm.get("shape")+"       coords:"+hm.get("coords"));   
  27. //              }   
  28.             }   
  29.         }   
  30.         catch (Exception pe)   
  31.         {   
  32.             pe.printStackTrace ();   
  33.         }   
  34.     }   
  35.        
  36.     /**  
  37.      * 得到指定文件夹下的所有符合规则的文件(本地)  
  38.      * */  
  39.     public static ArrayList getFileList(String content)throws Exception{   
  40.         URI uri = new URI(content);   
  41.         ArrayList al = new ArrayList();   
  42.         File file = new File(uri);   
  43.         System.out.println(file.exists());   
  44.         if(file.isDirectory()){   
  45.             File[] filelist = file.listFiles();   
  46.             for(int i=0;i<filelist.length;i++){   
  47.                 if(filelist[i].getName().substring(04).equals("JMO_")&&getFileTypeName(filelist[i].getName())){   
  48.                     al.add(filelist[i]);   
  49.                 }   
  50.             }   
  51.         }   
  52.         return al;   
  53.     }   
  54.        
  55.     public static boolean getFileTypeName(String strFile){   
  56.         if(strFile.substring(strFile.indexOf(".")+1, strFile.length()).equals("htm")||strFile.substring(strFile.indexOf(".")+1, strFile.length()).equals("html")){   
  57.             return true;   
  58.         }else{   
  59.             return false;   
  60.         }   
  61.     }   
  62.         
  63.     /**  
  64.      * 读取房态图中的单元号,房间大小,area位置  
  65.      * */  
  66.     public static ArrayList readByHtml(String content) throws Exception{   
  67.         ArrayList alRoom = new ArrayList();   
  68.         Parser parser = new Parser();   
  69.         parser.setEncoding("8859_1");   
  70.         parser.setInputHTML(getWmlContent(content));   
  71.   
  72.         PrototypicalNodeFactory factory = new PrototypicalNodeFactory ();   
  73.         factory.registerTag(new AreaTag ());   
  74.         parser.setNodeFactory(factory);   
  75.   
  76.         NodeList nlArea = parser.extractAllNodesThatMatch(lnkFilter);   
  77.         for(int i=0;i<nlArea.size();i++){   
  78.             CompositeTag node = (CompositeTag)nlArea.elementAt(i);   
  79.             if(node instanceof AreaTag){   
  80.                 AreaTag at = (AreaTag)nlArea.elementAt(i);   
  81.                 HashMap hm = new HashMap();   
  82.                 hm.put("href", at.getHref());   
  83.                 hm.put("shape", at.getShape());   
  84.                 hm.put("coords", at.getCoords());   
  85.                 alRoom.add(hm);   
  86.             }   
  87.         }   
  88.         return alRoom;   
  89.     }   
  90.        
  91.     /**  
  92.      * 得到文件中的内容  
  93.      * */  
  94.     static String getWmlContent(String content) throws Exception{   
  95.         StringBuffer wml = new StringBuffer();   
  96.         String line = getRemoteInfo(content);   
  97.         if(wml.length()>0)   
  98.             wml.append("\r\n");   
  99.         wml.append(line);    
  100.         return wml.toString();             
  101.     }   
  102.        
  103.     /**  
  104.      * 得到url文件的最后修改时间  
  105.      * */  
  106.     public static String getRemoteLastModified(String content)throws Exception{   
  107.         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
  108.         URL url = new URL(content);   
  109.         java.util.Date dateLast = new java.util.Date(url.openConnection().getLastModified());   
  110.         String strMod = sdf.format(dateLast);   
  111.         System.out.println(strMod);   
  112.         return strMod;   
  113.     }   
  114.        
  115.     /**  
  116.      * 获取远程html文件的内容  
  117.      * */  
  118.     public static String getRemoteInfo(String content)throws Exception{   
  119.         URL urlfile;   
  120.         BufferedReader in;   
  121.         String inputLine;   
  122.         String info = "";   
  123.         try {   
  124.              urlfile = new URL(content);   
  125.              in = new BufferedReader(new InputStreamReader(urlfile.openStream()));   
  126.              inputLine = in.readLine();   
  127.              while (inputLine != null) {   
  128.              info += inputLine ;   
  129.              inputLine = in.readLine();   
  130.              }   
  131.              in.close();   
  132.         } catch (MalformedURLException e) {   
  133.              // TODO Auto-generated catch block   
  134.              e.printStackTrace();   
  135.         } catch (IOException e){   
  136.              // TODO Auto-generated catch block   
  137.              e.printStackTrace();   
  138.         }   
  139.         return info;   
  140.   
  141.     }   
  142.        
  143.     /**  
  144.      * 获取远程目录中的文件名称  
  145.      * */  
  146.     public static ArrayList getRemoteDirInfo(String content)throws Exception{   
  147.         ArrayList alFile = new ArrayList();   
  148.         URL urlfile;   
  149.         BufferedReader in;   
  150.         String inputLine;   
  151.         String info = "";   
  152.         try {   
  153.              urlfile = new URL(content);   
  154.              in = new BufferedReader(new InputStreamReader(urlfile.openStream()));   
  155.              inputLine = in.readLine();   
  156.              while (inputLine != null) {   
  157.                  if(inputLine.substring(0,4).equals("JMO_")&&getFileTypeName(inputLine)){   
  158.                      alFile.add(inputLine);   
  159.                  }   
  160.                  inputLine = in.readLine();   
  161.              }   
  162.              in.close();   
  163.         } catch (MalformedURLException e) {   
  164.              // TODO Auto-generated catch block   
  165.              e.printStackTrace();   
  166.         } catch (IOException e){   
  167.              // TODO Auto-generated catch block   
  168.              e.printStackTrace();   
  169.         }   
  170.         return alFile;   
  171.   
  172.     }   
  173.   
  174.        
  175.     static NodeFilter lnkFilter = new NodeFilter() {   
  176.         public boolean accept(Node node) {   
  177.             if(node instanceof AreaTag)   
  178.                 return true;   
  179.             return false;   
  180.         }   
  181.     };   
  182.   
  183.     /**  
  184.      * 定义area标签,用于查找area信息  
  185.      * */  
  186.     static class AreaTag extends CompositeTag{   
  187.         private static final String[] mIds = new String[] {"area"};   
  188.   
  189.          private static final String[] mEndTagEnders = new String[] {"map"};   
  190.         /**  
  191.          * Create a new text area tag.  
  192.          */  
  193.         public AreaTag (){   
  194.         }   
  195.   
  196.         /**  
  197.          * Return the set of names handled by this tag.  
  198.          * @return The names to be matched that create tags of this type.  
  199.          */  
  200.         public String[] getIds ()   
  201.         {   
  202.             return (mIds);   
  203.         }   
  204.            
  205.         public String[] getEnders (){   
  206.             return (mIds);   
  207.         }   
  208.   
  209.         public String[] getEndTagEnders (){   
  210.             return (mEndTagEnders);   
  211.         }   
  212.            
  213.         public String getHref(){   
  214.             return super.getAttribute("href");   
  215.         }   
  216.            
  217.         public String getCoords(){   
  218.             return super.getAttribute("coords");   
  219.         }   
  220.            
  221.         public String getShape(){   
  222.             return super.getAttribute("shape");   
  223.         }   
  224.            
  225.         public String toString(){   
  226.             return mIds[0].toString();   
  227.         }   
  228.   
  229.     }   
  230.   
  231. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值