求一个xml声明中编码的正则表达式 <?xml version="1.0" encoding="GB2312" standalone="no"?>比如上面这个xml文件编码为gb2312,我将其通过string转换成新的utf-8编码xml文。现在需要替换表头声明的GB2312为UTF-8,<?xml version="1.0" encoding="UTF-8" standalone="no"?> ,求一个正则表达式。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 字符串替换就行,表达式就是encoding="GB2312" 不行的,很多比如gib5什么的编码,我全部转utf8 public static String findEncoding(File f)throws Exception{ BufferedReader reader=null; String line=" "; String encoding=" "; try{ reader=new BufferedReader(new InputStreamReader(new FileInputStream(f))); do{ line=reader.readLine(); }while(line!=null&&!line.startsWith("<?xml")); }catch(Exception e){ e.printStackTrace(); try{ reader.close(); }catch(Exception ee){ ee.printStackTrace(); } } if(line==null) line=" "; Pattern pattern=Pattern.compile("encoding\\s*=\\s*\"(\\w+[-,_]?){1,}\""); Matcher matcher = pattern.matcher(line.toLowerCase()); if(matcher.find()){ encoding=matcher.group(); } Pattern last=Pattern.compile("\"(\\w+[-,_]?){1,}\""); Matcher m=last.matcher(result); if(m.find()){ return result.substring(m.start()+1,m.end()-1); } else{ return " "; }}xml标准要求声明必须是第一行 关于图形的问题! 大家看看 急求 :jsp 开发tomcat,sql server 不是逼不得已我不会再次发帖求人 求求你们了嘛 行行好 !!!能用java编出截取在线播放的文件数据吗??!! jbuilder 中 有哪些快捷键可以方便的使用,大家都提提平时常用哪些? 帮忙做两个线程的题,谢谢!急急... 多类的dos运行! 请问如何把java文件编译成exe文件 用JBuilder8新建一Application,取名为:Test1,其它任何都是默认的,点击完成后出错。 InitialContext类的lookup方法 两个组件实现事件的同步问题 新手问题,请前辈们多多帮忙,谢谢
BufferedReader reader=null;
String line=" ";
String encoding=" ";
try{
reader=new BufferedReader(new InputStreamReader(new FileInputStream(f)));
do{
line=reader.readLine();
}while(line!=null&&!line.startsWith("<?xml"));
}catch(Exception e){
e.printStackTrace();
try{
reader.close();
}catch(Exception ee){
ee.printStackTrace();
}
}
if(line==null)
line=" ";
Pattern pattern=Pattern.compile("encoding\\s*=\\s*\"(\\w+[-,_]?){1,}\"");
Matcher matcher = pattern.matcher(line.toLowerCase());
if(matcher.find()){
encoding=matcher.group();
}
Pattern last=Pattern.compile("\"(\\w+[-,_]?){1,}\"");
Matcher m=last.matcher(result);
if(m.find()){
return result.substring(m.start()+1,m.end()-1);
}
else{
return " ";
}
}xml标准要求声明必须是第一行