1. 程式人生 > >NekoHTML解析HTML為XML後TagName一直為大寫的問題解決

NekoHTML解析HTML為XML後TagName一直為大寫的問題解決

問題:

java使用NekoHTML解析HTML的時候發現NekoHTML總是把標籤名轉換成大寫,導致之前寫的XPath都用不了,雖然可以用指令碼把之前的歷史XPath都轉換一遍,但是如果新來的運營不知道的話,還是可能會出現不必要的麻煩。

分析:

在網上一頓搜尋,發現自己的blog裡也有寫,只是之前沒有注意,NekoHTML提供了一些配置項,可以精確的配置NekoHTML的行為。 與我們這個問題相關的配置是:
DOMParser parser = new DOMParser();
parser.setProperty("http://cyberneko.org/html/properties/names/elems", "match");
//解析HTML檔案
parser.parse("http://www.baidu.com");
 //獲取解析後的DOM樹
Document document = parser.getDocument();

設定以後發現竟然沒有用,關鍵是NekoHTML的官網也上不去,不知道是被牆了還是怎麼。後來幸好在github找到一份映象,找到了文件。 文件中這麼寫著:

Why are the DOM element names always uppercase?

The HTML DOM specification explicitly states that element and attribute names follow the semantics, including case-sensitivity, specified in the HTML 4specification. In addition, 

section 1.2.1 of the HTML 4.01 specification states:

Element names are written in uppercase letters (e.g., BODY). Attribute names are written in lowercase letters (e.g., lang, onsubmit).

The Xerces HTML DOM implementation (used by default in the NekoHTML DOMParser class) follows this convention. Therefore, even if the "http://cyberneko.org/html/properties/names/elems" property is set to "lower", the DOM will still uppercase the element names.

To get around this problem, instantiate a Xerces2 DOMParser object using the NekoHTML parser configuration. By default, the Xerces DOM parser class creates a standard XML DOM tree, not an HTML DOM tree. Therefore, the element and attribute names will follow the settings for the "http://cyberneko.org/html/properties/names/elems" and "http://cyberneko.org/html/properties/names/attrs" properties. However, realize that the application will not be able to cast the document nodes to the HTML DOM interfaces for accessing the document's information.

The following sample code shows how to instantiate a DOM parser using the NekoHTML parser configuration:

// import org.apache.xerces.parsers.DOMParser;
// import org.cyberneko.html.HTMLConfiguration;

DOMParser parser = new DOMParser(new HTMLConfiguration());
大意就是說為了符合HTML4.01標準,NekoHTML會將TagName轉換為大寫,無論是否設定剛才說的配置項。解決辦法就是使用

org.apache.xerces.parsers.DOMParser代替原來的DOMParser。具體程式碼看下面的解決方案吧

解決方案:

直接插程式碼了:
        HTMLConfiguration htmlConfiguration = new HTMLConfiguration();
        htmlConfiguration.setProperty("http://cyberneko.org/html/properties/names/elems", "match");
        org.apache.xerces.parsers.DOMParser parser = new org.apache.xerces.parsers.DOMParser(htmlConfiguration);
        InputSource inputSource = new InputSource("http://www.baidu.com");
        parser.parse(inputSource);
        System.out.println(parser.getXMLParserConfiguration().getProperty("http://cyberneko.org/html/properties/names/elems"));
        //獲取解析後的DOM樹
        Document document = parser.getDocument();
        String xml = new XMLDocument(document).toString();
        System.out.println(xml);
附上pom.xml的相關依賴
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>


相關推薦

NekoHTML解析HTMLXMLTagName一直大寫的問題解決

問題: java使用NekoHTML解析HTML的時候發現NekoHTML總是把標籤名轉換成大寫,導致之前寫的XPath都用不了,雖然可以用指令碼把之前的歷史XPath都轉換一遍,但是如果新來的運營不

解決PyCharm下python使用XPath解析html,獲取文字時中文亂碼問題

最近在學習XPath解析庫,但是獲取中文文字時總是亂碼,網上看了些教程,然並卵,最後只好自己解決: 文字檔案html.txt如下: <p class="name"> <a href="/films/1297" title="肖申克的救贖" d

mui 之ajax遇到的坑 後臺接受資料端服務php

mui.ajax的官方模板為             mui.ajax(Host+'/app/index/newsinfo',{                 data:{id:newsId},                 dataType:'json',//伺服器返回

php將xml文件轉換html

php將xml文件轉換為htmltest.xml: <?xml version="1.0" encoding="ISO-8859-1"?> <catalog> <cd> <title>Empire Burlesque</title> <arti

使用dataset讀取xml 用dataview排序時什麽不是按數字類型排序 MQsz

父親 dataview 重要 找我 con 帶來 權力 view 尋找 <p>  秋天,葉子一片片落下,帶著一絲絲的遺憾,投向大地母親的懷抱。她們跳躍著,旋轉,著,輕舞飛揚著,翩然落下。仲春時節,輕寒料峭。一個朋友的父親專程從鄉下來縣城看我,他給我帶來了滿滿的一

C#:讀取html模板檔案,並替換修改檔案中指定值,儲存修改的檔案

1.準備html模板檔案:Pages/Device/DeviceModel8.html   2 using System.IO:讀取檔案內容,並替換指定內容                  &nbs

有關於htmljsp格式不對的問題

今天把寫好的程式碼複製貼上到開發工具後,在轉jsp後,發現頁面出現格式出錯問題。經過查詢資料後,我用下面的方法解決了這個問題。 把jsp的檔案頭改為下面這樣 <%@ page language="java" import="java.util.*" pageEnco

unittest 測試報告輸出xmlhtml,log

pytest框架相容unittest,而pytest生成報告可以通過命令完成 同樣適用於unittest 1、生成junitxml格式 pytest --junitxml report.xml test.py pytest -v test.py --junitxml

nginx 偽靜態,沒有綴名的url新增html字尾

剛做了官網,官網都是html檔案,領導要求訪問時隱藏.html字尾。伺服器用的是nginx,這樣需求的解決方案是修改配置檔案: location / { //新增上以下程式碼: if

java對xml全面解析,增,刪,改,以及將java物件重新編組xml檔案

{            //利用文件節點建立一個DOM輸入源            DOMSource source=new DOMSource(doc);            //以newXMLfile.xml建立一個StreamResult物件            StreamResult resu

使用Jacob批量轉換wordtxt、pdf、xps、htmlxml等文件

Jacob全稱位java com bridge,通過該外掛,可以使用Java語言編寫程式,呼叫COM、ActiveX元件來操作Windows本地程式。 參考一位網友的例子,我寫了一個程式,用來將word批量轉換為txt等格式的文件。 該程式核心部分,僅僅是呼叫了Jacob的

C# xml通過xslt轉換html輸出

html效果截圖: 1、首先分析html程式碼結構: 結果如圖: 2、呼叫介面返回的資料格式: 3、由第一步可看出每2個數據為一行並排顯示,後臺返回的資料總數可能為奇數個或偶數個,對應該生成的xml結構: 4、對應的xslt檔案程式碼 <?xml ver

爬蟲學習4-HTMLXML資料的分析與解析

目前在 Java 中,解析 HTML 工具主要包含以下幾種: 1,jsoup:強大的 HTML 解析工具,支援以 jQuery 中 CSS Selector 的方式提取 HTML 中的元素,學習成本較低。, 2,HtmlCleaner:另外一款開源的 Java 語言的 HTM

ajax返回值xml -解析

ajax返回值頁面資料 <?xml version="1.0" encoding="UTF-8"?> <root> <message>123</message> <url>www.baidu.com<

使用SAX解析xml的檔案儲存java物件

轉載自:http://blog.csdn.net/kingsonyoung/article/details/5199508 在Java使用Sax解析xml檔案中,我們介紹瞭如何用SAX解析xml檔案,接下來我們繼續學習如何將一個xml檔案的內容結構儲存到一個jav

【網路爬蟲】【java】微博爬蟲(四):資料處理——jsoup工具解析html、dom4j讀寫xml

        之前提到過,對於簡單的網頁結構解析,可以直接通過觀察法、手工寫正則解析,可以做出來,比如網易微博。但是對於結構稍微複雜點的,比如新浪微博,如果還用正則,用眼睛一個個去找,未免太麻煩了。         本文介紹兩個工具包:解析html, xml的jsoup,

【Android】pull解析xml檔案+將資料儲存xml格式,並儲存在記憶體裡

在解析中,常用到的還有一種解析就是pull去解析xml格式的檔案。事實上android內部也是這樣做的。今天這個demo是來自傳智播客,可能技術已經被翻新了。 但是基礎原理還是那樣,希望基礎學習者,能夠理解,並實際寫一寫。 首先在src目錄下匯入我們將要解析的xml檔案:

dicom之將dcm檔案解析jpg等比例壓縮

前段時間用dcm4chee對dcm檔案進行了轉換,轉換格式為bmp和jpg,但是轉化完後發現6M的dcm原始檔變成了一個13M的bmp,手機端顯示會很吃力,於是決定做個壓縮處理,提供兩種壓縮方式,第一種方式的壓縮時間會比較長,第二種就比較好:code1:package co

通過使用jsoup解析html,繪畫表格生成execl文件

num group wid 字符 for format 格式 colspan tables 1.獲取文件或者字符設置繪畫表格字符編碼 //得到Document並且設置編碼格式 public static Document getDoc(String fileNam

python 解析html網頁

class find() [] index file 字符 .com 查找 cto pyquery庫是jQuery的Python實現,可以用於解析HTML網頁內容,使用方法: 代碼如下: from pyquery import PyQuery as pq 1、可加載一段H