现在的位置: 首页 > 综合 > 正文

“用dom4j解析包”对xml进行dom方式操作中文乱码解决方案

2017年11月19日 ⁄ 综合 ⁄ 共 4309字 ⁄ 字号 评论关闭

IT程序员开发必备-各类资源下载清单,史上最全IT资源,个人收藏总结!

package edu.dom4j.dom;

import java.io.FileOutputStream;

import java.io.FileWriter;

import java.io.OutputStreamWriter;

import java.io.IOException;

import org.dom4j.Attribute;

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.Node;

import org.dom4j.io.OutputFormat;

import org.dom4j.io.SAXReader;

import org.dom4j.io.XMLWriter;

import org.junit.Test;

public class XmlCharset {

  private String xmlfile = "WebRoot/product2.xml";

  private  Attribute attribute;

  private Node node ;

  public Document getDocument () throws DocumentException {

   //获得dom4j解析器

   SAXReader reader = new SAXReader();

   //读取xml文档:注意是org.dom4j.Document包下

   Document document = reader.read(xmlfile);

   return document;

  }

 /**

  * 运行环境:

  *   (1)product2.xml中encoding="utf-8" 或者encoding="gb2312"(是否乱码结果一样)

 *   (2)本地编码:gb2312

  **/

  @Test

  public void formatXml2system_1() throws DocumentException, IOException{

   Document document = getDocument();

   /*

    * 1.将document对象以Compact format的方式打印到System.out

    *  结果:  OK

    */

   OutputFormat format = OutputFormat.createCompactFormat();

   format.setEncoding("gb2312");

   XMLWriter writer = new XMLWriter(System.out,format);

   writer.write(document);

   writer.flush();

   /*

    *  结果:  乱码

    */

   format = OutputFormat.createCompactFormat();

   format.setEncoding("utf-8");

   writer = new XMLWriter(System.out,format);

   writer.write(document);

  writer.flush();

   /*

    * 结果:  OK

    */

   format = OutputFormat.createCompactFormat();

   writer = new XMLWriter(new OutputStreamWriter(System.out,"gb2312"),format);

  writer.write(document);

   writer.flush();

 

   /*

    *  结果: OK

    */

  format = OutputFormat.createCompactFormat();

   format.setEncoding("utf-8");

   writer = new XMLWriter(new OutputStreamWriter(System.out,"gb2312"),format);

   writer.write(document);

   writer.flush();

   /*

    *  结果: 乱码

    */

   format = OutputFormat.createCompactFormat();

   writer = new XMLWriter(new OutputStreamWriter(System.out,"utf-8"),format);

   writer.write(document);

   writer.flush();

   /*

   *  结果:OK

   */

   format = OutputFormat.createCompactFormat();

   writer = new XMLWriter(new OutputStreamWriter(System.out),format);

   writer.write(document);

   writer.flush();

  

  }

  /**

   * 运行环境:

   *   (1)product2.xml中encoding="utf-8" 或者encoding="gb2312"(是否乱码结果一样)

   *      但是生成的product3.xml的encoding=""编码由format的编码决定

   *   (2)本地编码:gb2312

   **/

  @Test

  public void formatXml2system_2() throws DocumentException, IOException{

   Document document = getDocument();

   String product3 = "WebRoot/product3.xml";

  

   /*

   * 1.将document对象更新至product2.xml或新建xml

   *   结果:乱码

   */

   OutputFormat format = OutputFormat.createPrettyPrint();

   XMLWriter writer = new XMLWriter(new FileWriter(product3),format);

   writer.write(document);

   writer.flush();

  /*

   *   结果:乱码

   */

   format = OutputFormat.createPrettyPrint();

   format.setEncoding("utf-8");

   writer = new XMLWriter(new FileWriter(product3),format);

   writer.write(document);

   writer.flush();

  /*

   *   结果 :OK

   */

   format = OutputFormat.createPrettyPrint();

   format.setEncoding("gb2312");

   writer = new XMLWriter(new FileWriter(product3),format);

   writer.write(document);

   writer.flush();

  /*

   *   结果:OK

   */

   format = OutputFormat.createPrettyPrint();

   writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(product3),"utf-8"));

   writer.write(document);

   writer.flush();

  /*

   *   结果:乱码

   */

   writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(product3),"gb2312"));

   writer.write(document);

   writer.flush();

 

  /*

   *   结果:OK

   */

   format = OutputFormat.createPrettyPrint();

   writer = new XMLWriter(new FileOutputStream(product3));

   writer.write(document);

   writer.flush();

  /*

   *   结果:OK 

   */

   format = OutputFormat.createPrettyPrint();

   //product3.xml中生成的encoding=""编码由format决定

   format.setEncoding("gb2312");

   writer = new XMLWriter(new FileOutputStream(product3),format);

   writer.write(document);

   writer.flush();

  }

}

product2.xml文档:

<?xml version="1.0" encoding="utf-8"?>

<catalog id="cata1"> 

  <product category="HandTool" inventory="InStock"> 

    <specifications weight="2.0kg">扳手</specifications>  

    <price street="香港街">80.0</price>  

    <notes>这是扳手</notes>  

  </product>  

  <product category="Table"> 

    <specifications>桌椅</specifications>  

    <price street="澳门街" wholesale="部分">100.0</price> 

  </product> 

</catalog>




抱歉!评论已关闭.