日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

dom4j處理中文之編碼問題_

 孫中熙——路 2011-03-22
查看文章
 
dom4j處理中文之編碼問題
2009-05-08 11:44

問題描述

在使用dom4j的時(shí)候發(fā)現(xiàn)有時(shí)會(huì)出現(xiàn)這樣一個(gè)問題:無法以UTF-8編碼格式成功保存xml文件,具體表現(xiàn)為保存后中文呈現(xiàn)亂碼(如果沒有亂碼,說明保存前的編碼沒有設(shè)置成功,保存成了本地的gbk或者gb2312格式)再次讀取的時(shí)候會(huì)報(bào)類似如下的錯(cuò)誤:

Invalid byte 2 of 2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2-byte UTF-8 sequence.

Invalid byte 1 of 1-byte UTF-8 sequence. Nested exception: Invalid byte 1 of 1-byte UTF-8 sequence.

Invalid byte 2 of 2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2-byte UTF-8 sequence.

Invalid byte 2 of 2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2-byte UTF-8 sequence.

Invalid byte 1 of 1-byte UTF-8 sequence. Nested exception: Invalid byte 1 of 1-byte UTF-8 sequence.

dom4j的范例中新建一個(gè)xml文檔的代碼如下:

// 輸出XML文檔

try

{

    XMLWriter output = new XMLWriter(new FileWriter(new File("data/catalog.xml")));

    output.write(document);

    output.close();

}

catch (IOException e)

{

    System.out.println(e.getMessage());

}

錯(cuò)誤原因分析
   
在上面的代碼中輸出使用的是FileWriter對(duì)象進(jìn)行文件的輸出。這就是不能正確進(jìn)行文件編碼的原因所在,Java中由Writer類繼承下來的子類沒有提供編碼格式處理,所以dom4j也就無法對(duì)輸出的文件進(jìn)行正確的格式處理。這時(shí)候所保存的文件會(huì)以系統(tǒng)的默認(rèn)編碼對(duì)文件進(jìn)行保存,在中文版的windowJava的默認(rèn)的編碼為GBK,也就是說雖然我們標(biāo)識(shí)了要將xml保存為utf-8格式,但實(shí)際上文件是以GBK格式來保存的,所以這也就是為什么我們使用GBK、GB2312編碼來生成xml文件能正確的被解析,而以UTF-8格式生成的文件不能被xml解析器所解析的原因。

如何解決問題?

首先我們看看dom4j是如何實(shí)現(xiàn)編碼處理的,如下所示:
public XMLWriter(OutputStream out) throws UnsupportedEncodingException {

    //System.out.println("In OutputStream");

    this.format = DEFAULT_FORMAT;

    this.writer = createWriter(out, format.getEncoding());

    this.autoFlush = true;

   namespaceStack.push(Namespace.NO_NAMESPACE);

}

public XMLWriter(OutputStream out, OutputFormat format) throws UnsupportedEncodingException {

    //System.out.println("In OutputStream,OutputFormat");

    this.format = format;

    this.writer = createWriter(out, format.getEncoding());

    this.autoFlush = true;

   namespaceStack.push(Namespace.NO_NAMESPACE);

}

/**

* Get an OutputStreamWriter, use preferred encoding.

*/

protected Writer createWriter(OutputStream outStream, String

    encoding) throws UnsupportedEncodingException {

    return new BufferedWriter(

        new OutputStreamWriter( outStream, encoding )

    );

}

由上面的代碼我們可以看出dom4j對(duì)編碼并沒有進(jìn)行什么很復(fù)雜的處理,完全通過 Java本身的功能來完成。所以我們?cè)谑褂?/span>dom4j生成xml文件時(shí)不應(yīng)該直接在構(gòu)建XMLWriter時(shí),為其賦一個(gè)Writer對(duì)象,而應(yīng)該通過一個(gè)OutputStream的子類對(duì)象來構(gòu)建。也就是說在我們上面的代碼中,不應(yīng)該用FileWriter對(duì)象來構(gòu)建xml文檔,而應(yīng)該使用 FileOutputStream對(duì)象來構(gòu)建,修改后的代碼如下:

// 輸出XML文檔

try

{

    OutputFormat outFmt = new OutputFormat("\t", true);

    outFmt.setEncoding("UTF-8");

    XMLWriter output = new XMLWriter(new FileOutputStream(filename), outFmt);  

    output.write(document);

    output.close();

}

catch (IOException e)

{

    System.out.println(e.getMessage());

}

如何讀取呢?

public List extractXMLText(File inputXml, String node)

{

    List texts = null;

    try

    {

       // 使用SAXReader解析XML文檔,SAXReader包含在org.dom4j.io包中。

       // inputXml是由xml文件創(chuàng)建的java.io.File。

       SAXReader saxReader = new SAXReader();

       saxReader.setEncoding("UTF-8");

       Document document = saxReader.read(inputXml);

          

       texts = document.selectNodes(node); // 獲取sentence列表

    }

catch (DocumentException e)

  {

       System.out.println(e.getMessage());

  }

  return texts;

}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(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)論公約

    類似文章 更多