亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

使用了InputStreamReader進行編碼轉換,但是輸出的漢字還是亂碼

package com.imooc.jdomtest;


import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.util.List;


import org.jdom2.Attribute;

import org.jdom2.Document;

import org.jdom2.Element;

import org.jdom2.JDOMException;

import org.jdom2.input.SAXBuilder;



public class JDOMTest {


/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

//進行對books.xml文件的JDOM解析

//準備工作

//1.創建一個SAXBuilder對象

SAXBuilder saxBuilder = new SAXBuilder();


InputStream in;

try {

//2.創建一個輸入流,將XML文件加載到輸入流中來

in = new FileInputStream("src/res/books.xml");

InputStreamReader isr =new InputStreamReader(in, "UTF-8");

//3.通過saxBuilder的build方法,將輸入流加載到saxBuilder中

Document document= saxBuilder.build(isr);

//4.通過document對象獲取xml文件的根節點

Element rootelement = document.getRootElement();

//5.獲取根節點下的子節點的List集合

List<Element> bookList = rootelement.getChildren();

//繼續進行解析

for (Element book : bookList) {

System.out.println("=======開始解析第"

+ (bookList.indexOf(book) + 1) + "書=======");

//解析book的屬性

List<Attribute> attrList = book.getAttributes();

//知道節點下屬性名稱時,獲取屬性值

// book.getAttributeValue("id");

//遍歷attrList(針對不清楚book節點下屬性的名字及數量)

for (Attribute attr : attrList) {

//獲取屬性名

String attrName = attr.getName();

//獲取屬性值

String attrValue = attr.getValue();

System.out.println("屬性名:" + attrName +"---屬性值:" +attrValue);

}

//對book節點的子節點的節點名以及節點值的遍歷

List<Element> bookChilds = book.getChildren();

for (Element child : bookChilds) {

System.out.println("節點名:" + child.getName() + "------節點值:"

+ child.getValue());

}

System.out.println("=======結束解析第"

+ (bookList.indexOf(book) + 1) + "書=======");

}

}catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}catch (JDOMException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


}


正在回答

1 回答

InputStreamReader isr =new InputStreamReader(in, "UTF-8");--->InputStreamReader isr =new InputStreamReader(in, "gbk");? 就可以了

因為項目的Resource---->Text file encodeing---->Inherited from container (GBK)

0 回復 有任何疑惑可以回復我~
#1

阿西莫夫 提問者

項目的Resource---->Text file encodeing---->Inherited from container (GBK),的確為默認GBK 把new InputStreamReader(in, "gbk"); 設置為gbk也沒用,依然亂碼。 嘗試只有新建項目時,項目的Resource---->Text file encodeing---->Inherited from container(UTF-8),才能正常轉換編碼格式為ISO-8859-1的books.xml文件編碼
2017-12-10 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消

使用了InputStreamReader進行編碼轉換,但是輸出的漢字還是亂碼

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號