使用NekoHtml處理網頁(刪除Style標籤)

來源:互聯網
上載者:User

標籤:java   html   

最近在做一個手機APP,通過一個新聞抓取程式抓取新聞,然後通過APP展示新聞。後發現手機端不支援Style標籤,如果網頁中有Style標籤,則標籤內的內容會顯示處理非常影響頁面美觀。於是就寫了一個用NekoHTML來清除Style標籤的工具類


html.filter.properties 設定檔,配置允許的標籤和要刪除的標籤及標籤內的屬性

attributes=style,id,name,class,width,height,src,oldsrc,complete,align,alt,titleacceptTags=div,span,a,li,ul,nav,br,p,img,font,b,strong,table,tr,tdremoveTags=style

PropertiesUtils 讀取Properties

package com.tiamaes.gjds.util;import java.io.IOException;import java.util.Properties;import org.springframework.core.io.ClassPathResource;/**   * <p>類描述: 讀取Properties中的屬性 </p> * <p>建立人:王成委  </p> * <p>建立時間:2015年1月28日 上午11:23:27  </p> * <p>著作權說明: © 2015 Tiamaes </p> */public class PropertiesUtils {private Properties properties;public PropertiesUtils(String path){try {ClassPathResource resource = new ClassPathResource(path);properties = new Properties();properties.load(resource.getInputStream());} catch (IOException e) {e.printStackTrace();}}public String get(String key){return this.properties.getProperty(key);}}

過濾HTML中的標籤

package com.tiamaes.gjds.util;import java.io.CharArrayReader;import java.io.StringWriter;import java.util.ArrayList;import java.util.List;import org.apache.xerces.xni.parser.XMLDocumentFilter;import org.cyberneko.html.filters.ElementRemover;import org.cyberneko.html.filters.Writer;import org.cyberneko.html.parsers.DOMParser;import org.xml.sax.InputSource;/**   * <p>類描述: 過濾Html中的標籤 </p> * <p>建立人:王成委  </p> * <p>建立時間:2015年1月29日 上午10:45:02  </p> * <p>著作權說明: © 2015 Tiamaes </p> */public class HtmlFilterUtils {private static PropertiesUtils properties = null;private static HtmlFilterUtils filter = null;private String configPath = "html.filter.properties";private static final String ATTRIBUTE_FIELD = "attributes";private static final String ACCEPT_TAGS_FIELD = "acceptTags";private static final String REMOVE_TAGS_FIELD = "removeTags";private List<String> attributes = new ArrayList<String>();private List<String> acceptTags = new ArrayList<String>();private List<String> removeTags = new ArrayList<String>();private static synchronized void syncInit(){if(filter == null)filter = new HtmlFilterUtils();}public static HtmlFilterUtils getInstance(){return getInstance(false);}public static HtmlFilterUtils getInstance(boolean createNew){if(createNew)return new HtmlFilterUtils();if(filter == null){syncInit();}return filter;}private HtmlFilterUtils(){if(properties == null){properties = new PropertiesUtils(configPath);}this.addToList(attributes, properties.get(ATTRIBUTE_FIELD));this.addToList(acceptTags, properties.get(ACCEPT_TAGS_FIELD));this.addToList(removeTags, properties.get(REMOVE_TAGS_FIELD));}public void addAtributes(String attrName){this.attributes.add(attrName);}public void removeAtributes(String attrName){this.attributes.remove(attrName);}public void addRmoveTag(String tagName){this.removeTags.add(tagName);}public void removeRmoveTag(String tagName){this.removeTags.remove(tagName);}public void addAcceptTag(String tagName){this.acceptTags.add(tagName);}public void removeAcceptTag(String tagName){this.acceptTags.remove(tagName);}private void addToList(List<String> list,String sources){if(list == null) list = new ArrayList<String>();String[] sourcesArray = sources.split(",");for(String str:sourcesArray){list.add(str);}}public String doFilter(String htmlCode){ElementRemover remover = new ElementRemover();String[] atrrs = new String[attributes.size()];for(String tag : acceptTags)remover.acceptElement(tag,attributes.toArray(atrrs));for(String tag : removeTags)remover.removeElement(tag);CharArrayReader reader = null;String result;try {StringWriter filteredDescription = new StringWriter();Writer writer = new Writer(filteredDescription,"UTF-8");XMLDocumentFilter[] filters = {remover,writer};DOMParser parser = new DOMParser();reader = new CharArrayReader(htmlCode.toCharArray());InputSource inputSource = new InputSource(reader);parser.setProperty("http://cyberneko.org/html/properties/filters", filters);parser.parse(inputSource);result = filteredDescription.toString();} catch (Exception e1) {e1.printStackTrace();result =  htmlCode;}try {reader.close();} catch (Exception e) {e.printStackTrace();}return result;}}
調用doFilter可以過濾HTML的內容


使用NekoHtml處理網頁(刪除Style標籤)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.