关于java读取xml文件的问题

jancky2001 2009-07-08 05:11:47
我有一个xml文件,内容如下

<?xml version="1.0" encoding="UTF-8"?>
<smil xmlns="http://www.w3.org/2000/SMIL20/CR/Language">
<head />
<body>
<par dur="5000ms">
<img src="day_service.jpg" />
<text src="#dayservice" />
</par>
<par dur="5000ms">
<img src="day_area.jpg" />
<text src="#dayarea" />
</par>
<par dur="5000ms">
<img src="day_channel.jpg" />
<text src="#daychannel" />
</par>
</body>
</smil>


我用以下这个方法读取xml文件的内容:

public String readTxt(String filePathAndName,String encoding) throws IOException{
encoding = encoding.trim();
StringBuffer str = new StringBuffer("");
String st = "";
try{
FileInputStream fs = new FileInputStream(filePathAndName);
InputStreamReader isr;
if(encoding.equals("")){
isr = new InputStreamReader(fs);
}else{
isr = new InputStreamReader(fs,encoding);
}
BufferedReader br = new BufferedReader(isr);
try{
String data="" ;
while((data = br.readLine())!=null){
str.append(data+"\n");
}
}catch(Exception e){
str.append(e.toString());
}
st = str.toString();
}catch(IOException es){
es.printStackTrace();
st = "";
}
System.out.println("SSTT:"+st);
return st;
}


然后将得到st对象打印出来,我发现文件前面多了一个?(问号),请问是什么原因?

?<?xml version="1.0" encoding="UTF-8"?>
<smil xmlns="http://www.w3.org/2000/SMIL20/CR/Language">
<head />
<body>
<par dur="5000ms">
<img src="day_service.jpg" />
<text src="#dayservice" />
</par>
<par dur="5000ms">
<img src="day_area.jpg" />
<text src="#dayarea" />
</par>
<par dur="5000ms">
<img src="day_channel.jpg" />
<text src="#daychannel" />
</par>
</body>
</smil>

哪位高手正解下,谢谢了
...全文
27 点赞 收藏 3
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
arthtic 2009-07-08
[Quote=引用 2 楼 arthtic 的回复:]
? <?xml version="1.0" encoding="UTF-8"?>
<smil xmlns="http://www.w3.org/2000/SMIL20/CR/Language">
<head />
<body>
<par dur="5000ms">
<img src="day_service.jpg" />
<text src="#dayservice" />
</par>
<par dur="5000ms">
<img src="day_area.jpg" />
<text src="#dayarea" />
</par>
<par dur="5000ms">
<img src="day_channel.jpg" />
<text src="#daychannel" />
</par>

</body>

[/Quote]
无视吧....看错....
回复
arthtic 2009-07-08
? <?xml version="1.0" encoding="UTF-8"?>
<smil xmlns="http://www.w3.org/2000/SMIL20/CR/Language">
<head />
<body>
<par dur="5000ms">
<img src="day_service.jpg" />
<text src="#dayservice" />
</par>
<par dur="5000ms">
<img src="day_area.jpg" />
<text src="#dayarea" />
</par>
<par dur="5000ms">
<img src="day_channel.jpg" />
<text src="#daychannel" />
</par>
</body>
</smil>
楼主发的两个版本貌似一点点不同...
回复
csdn老杨 2009-07-08
1、你原来的那个文件的编码不是UTF8
2、你原来的那个文件前有字符,是那种不可见的字符。
回复
发帖
Java SE
创建于2007-09-28

6.1w+

社区成员

Java 2 Standard Edition
申请成为版主
帖子事件
创建了帖子
2009-07-08 05:11
社区公告
暂无公告