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內容轉成字元串。然後遍歷字元串。刪掉你不想要的字元就可以了額。。