A. java如何把string转为utf-8
java不同编码之间进行转换,都需要使用unicode作为中转。
String str = "任意字符串";
str = new String(str.getBytes("gbk"),"utf-8");
备注说明:
str.getBytes("UTF-8"); 意思是以UTF-8的编码取得字节
new String(XXX,"UTF-8"); 意思是以UTF-8的编码生成字符串
举例:
public static String getUTF8XMLString(String xml) {
// A StringBuffer Object
StringBuffer sb = new StringBuffer();
sb.append(xml);
String xmString = "";
String xmlUTF8="";
try {
xmString = new String(sb.toString().getBytes("UTF-8"));
xmlUTF8 = URLEncoder.encode(xmString, "UTF-8");
System.out.println("utf-8 编码:" + xmlUTF8) ;
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// return to String Formed
return xmlUTF8;
(1)javastringxml扩展阅读:
UTF-8编码规则:
如果只有一个字节则其最高二进制位为0;如果是多字节,其第一个字节从最高位开始,连续的二进制位值为1的个数决定了其编码的字节数,其余各字节均以10开头。
实际表示ASCII字符的UNICODE字符,将会编码成1个字节,并且UTF-8表示与ASCII字符表示是一样的。所有其他的UNICODE字符转化成UTF-8将需要至少2个字节。每个字节由一个换码序列开始。第一个字节由唯一的换码序列,由n位连续的1加一位0组成,首字节连续的1的个数表示字符编码所需的字节数。
Unicode转换为UTF-8时,可以将Unicode二进制从低位往高位取出二进制数字,每次取6位,如上述的二进制就可以分别取出为如下示例所示的格式,前面按格式填补,不足8位用0填补。
B. Java 如何解析由String类型拼接的XML格式
public static void main(String[] args) throws DocumentException {
String xml="<?xml version= '1.0' encoding='gb2312'?><weighData><weighTime>2012-12-2 12:23:12</weighTime><cardNum>2</cardNum><cfid>123</cfid></weighData>";
Document dom=DocumentHelper.parseText(xml);
Element root=dom.getRootElement();
String weighTime=root.element("weighTime").getText();
String cardNum=root.element("cardNum").getText();
String cfid=root.element("cfid").getText();
System.out.println(weighTime);
System.out.println(cardNum);
System.out.println(cfid);
}
//需要的包 dom4j.jar
C. 如何在java代码中获取string.xml中的字符串
获取string.xml文件里面的值有几个不同的地方。
1.在AndroidManifest.xml与layout等xml文件里:
android:text="@string/resource_name"
2.在activity里:
方法一:this.getString(R.string.resource_name);
方法二:getResources().getString(R.string.resource_name);
3.在其他java文件(必须有Context或pplication)
方法一:
context.getString(R.string.resource_name);
方法二: application.getString(R.string.resource_name);
D. java 读取excel文件 我需要去读到每一行的每一个数据。 然后操作一番。 最后生成一个XML文件
jxl
package com.cj.fw.xml;
import java.io.File;import java.io.IOException;
import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;
import jxl.Workbook;import jxl.WorkbookSettings;import jxl.read.biff.BiffException;import jxl.write.Label;import jxl.write.WritableCell;import jxl.write.WritableSheet;import jxl.write.WritableWorkbook;import jxl.write.WriteException;import com.cj.pro.domain.Drilling;
public class DrillingExport {
// 模板文件位置 private final static String TempletFile = "/attachments/template/emergencydrill.xls"; // 数据 private Drilling log = null; private String leixing=null;
/** * 构造函数 */ public DrillingExport( Drilling log,String leixing) { this.log=log; this.leixing=leixing; }
/** * 安全培训日志导出表 * @throws IOException * @throws BiffException * @throws WriteException */ public boolean export(HttpServletRequest request,HttpServletResponse response) throws BiffException, IOException, WriteException { String fileName=""; if("1".equals(leixing)){ fileName="应急演练信息导出表.xls"; }else if("2".equals(leixing)){ fileName="消防演练演练信息导出表.xls"; } response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-type","application/file"); response.setHeader("Content-Disposition", "attachment;filename=" + new String( fileName.getBytes("gb2312"), "ISO8859-1" )); File file = new File(request.getSession().getServletContext().getRealPath(TempletFile)); WorkbookSettings workbookSettings=new WorkbookSettings(); workbookSettings.setEncoding("ISO-8859-1"); //关键代码,解决中文乱码 Workbook workbook = Workbook.getWorkbook(file,workbookSettings); WritableWorkbook wwb = Workbook.createWorkbook(response.getOutputStream(), workbook); WritableSheet ws = wwb.getSheet(0); WritableCell cell = null; if("1".equals(leixing)){ cell = new Label(1,0,"应急演练单",ws.getCell(1,0).getCellFormat()); ws.addCell(cell); }else if("2".equals(leixing)){ cell = new Label(1,0,"消防演练单",ws.getCell(1,0).getCellFormat()); ws.addCell(cell); } if(log!=null){ cell = new Label(2,1,log.getRiqi(),ws.getCell(1,1).getCellFormat()); ws.addCell(cell); cell = new Label(5,1,log.getDidian(),ws.getCell(1,1).getCellFormat()); ws.addCell(cell); cell = new Label(2,2,log.getMingcheng(),ws.getCell(1,1).getCellFormat()); ws.addCell(cell); cell = new Label(5,2,log.getZudanwei(),ws.getCell(1,1).getCellFormat()); ws.addCell(cell); cell = new Label(2,3,log.getJiaoben(),ws.getCell(1,1).getCellFormat()); ws.addCell(cell); cell = new Label(2,4,log.getBeizhu(),ws.getCell(1,1).getCellFormat()); ws.addCell(cell); }else{ cell = new Label(2,4,"导出出错",ws.getCell(1,0).getCellFormat()); ws.addCell(cell); } //合计小写 wwb.write(); wwb.close(); workbook.close(); return true; }
}
上面的是导出 你可以变变
E. java读取XML文件
ByteArrayInputStream in = new ByteArrayInputStream(xmlString.getBytes("utf-8"));
SAXReader xmlReader = new SAXReader();
Document document = xmlReader.read(in);
Element root = document.getRootElement();
XPath x = document.createXPath("//ShowList/Movie");
List<Element> elements = (List<Element>)x.selectNodes(document);
for(Element el:elements){
String s= el.elementText("Name")
+" "+el.elementText("Poster")
+" "+el.elementText("Director")
+" "+el.elementText("Actor")
+" "+el.elementText("Type")
+" "+el.elementText("Price")
+" "+el.elementText("Type")
;
List<Element> scheles = (List<Element>)el.element("Schele").elements("Item");
for(Element el2:scheles){
System.out.println(" "+s+" "+el2.getText());
}
}
dom4j-1.6.1.jar
jaxen-1.1.1.jar
运行结果
非常完美perfect阴萌范冰冰Romance60Romance09:00
非常完美perfect阴萌范冰冰Romance60Romance13:00
星球大战perfect阴萌范冰冰Romance60Romance11:00
星球大战perfect阴萌范冰冰Romance60Romance16:00
D:java>
F. 用Java怎么把String类型的字符串转化为XML格式输出
java中将string转换成文件,使用开源jar包 dom4j:
packagecom.webdesk.swing.powertable.util;
importjava.io.ByteArrayInputStream;
importjava.io.File;
importjava.io.FileWriter;
importjava.io.IOException;
importorg.dom4j.Document;
importorg.dom4j.DocumentException;
importorg.dom4j.io.OutputFormat;
importorg.dom4j.io.SAXReader;
importorg.dom4j.io.XMLWriter;
publicclassXmlUtil{
(StringfileName){
try{
SAXReadersaxReader=newSAXReader();//新建一个解析类
DocumenttempDocument=saxReader.read(XmlUtil.class.getClassLoader().getResourceAsStream(fileName));//读入一个文件
returntempDocument.asXML();
}catch(DocumentExceptione){
e.printStackTrace();
}
returnnull;
}
//将字符串string类型转换成xml文件
publicstaticvoidstrChangeXML(Stringstr)throwsIOException{
SAXReadersaxReader=newSAXReader();
Documentdocument;
try{
document=saxReader.read(newByteArrayInputStream(str.getBytes("UTF-8")));
OutputFormatformat=OutputFormat.createPrettyPrint();
/**将document中的内容写入文件中*/
XMLWriterwriter=newXMLWriter(newFileWriter(newFile("src/com/webdesk/swing/powertable/digester/cctv.xml")),format);
writer.write(document);
writer.close();
}catch(DocumentExceptione){
e.printStackTrace();
}
}
}
G. java 中 String转xml 解析问题
怎么做我不知道。但是我觉得可以将真个xml内容转成字符串。然后遍历字符串。删掉你不想要的字符就可以了额。。