
2005年8月22日
、xml有哪些解析技術?區別是什么?
答:有DOM,SAX,STAX等
DOM:處理大型文件時其性能下降的非常厲害。這個問題是由DOM的樹結構所造成的,這種結構占用的內存較多,而且DOM必須在解析文件之前把整個文檔裝入內存,適合對XML的隨機訪問SAX:不現于DOM,SAX是事件驅動型的XML解析方式。它順序讀取XML文件,不需要一次全部裝載整個文件。當遇到像文件開頭,文檔結束,或者標簽開頭與標簽結束時,它會觸發一個事件,用戶通過在其回調事件中寫入處理代碼來處理XML文件,適合對XML的順序訪問
STAX:Streaming API for XML (StAX)
2、你在項目中用到了xml技術的哪些方面?如何實現的?
答:用到了數據存貯,信息配置兩方面。在做數據交換平臺時,將不能數據源的數據組裝成XML文件,然后將XML文件壓縮打包加密后通過網絡傳送給接收者,接收解密與解壓縮后再同XML文件中還原相關信息進行處理。在做軟件配置時,利用XML可以很方便的進行,軟件的各種配置參數都存貯在XML文件中。
3、用jdom解析xml文件時如何解決中文問題?如何解析?
答:看如下代碼,用編碼方式加以解決
package test;
import java.io.*;
public class DOMTest
{
private String inFile = "c:\people.xml";
private String outFile = "c:\people.xml";
public static void main(String args[])
{
new DOMTest();
}
public DOMTest()
{
try
{
javax.xml.parsers.DocumentBuilder builder =
javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder();
org.w3c.dom.Document doc = builder.newDocument();
org.w3c.dom.Element root = doc.createElement("老師");
org.w3c.dom.Element wang = doc.createElement("王");
org.w3c.dom.Element liu = doc.createElement("劉");
wang.appendChild(doc.createTextNode("我是王老師"));
root.appendChild(wang);
doc.appendChild(root);
javax.xml.transform.Transformer transformer =
javax.xml.transform.TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING, "gb2312");
transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT, "yes");
transformer.transform(new javax.xml.transform.dom.DOMSource(doc),
new
javax.xml.transform.stream.StreamResult(outFile));
}
catch (Exception e)
{
System.out.println (e.getMessage());
}
}
}
posted @
2005-08-22 15:08 jam 閱讀(1335) |
評論 (0) |
編輯 收藏

String subString(String str, int i)
{
String strReturn = "";

if (str.equals("") || str == null || i == 0)
{
return "";
}

for (int j = 1; j <= str.length(); j++)
{
String temp = "";
temp = str.substring(0, j);

if (temp.getBytes().length >= i)
{

if (temp.substring(j - 1, j).getBytes().length == 1)
{
strReturn = temp;
}

else
{
strReturn = str.substring(0, j - 1);
}

break;
}

}

return strReturn;
} 以上是java面試題:
“編程:編寫一個截取字符串的函數,輸入為一個字符串和字節數,輸出為按字節數
但是要保證漢字不被截半個,如“我ABC”4,應該截為“我AB”,輸入“我ABC漢DEF”,6,應該輸出為“我ABC”而不是“我ABC+漢的半個”。 ”
posted @
2005-08-22 14:25 jam 閱讀(6807) |
評論 (8) |
編輯 收藏
1
package test;
2
3
public class BigInt
{
4
public static void main(String arg[])
{
5
new BigInt();
6
}
7
8
public BigInt()
{
9
String bigInt1 =
10
"12312311111111111111111111111111111111113132135135465464646546455545";
11
String bigInt2 =
12
"12312311111111111111111111111111111111113132135135465464646546455545";
13
int[] iArr1 = arr(bigInt1);
14
int[] iArr2 = arr(bigInt2);
15
int[] iArr3 = new int[iArr2.length];
16
int iTemp = 0;
17
String flag = "";
18
for (int i = 0; i < iArr1.length; i++)
{
19
if (flag.equals("add"))
{
20
iTemp = iArr1[i] + iArr2[i] + 1; ;
21
}
22
else
{
23
iTemp = iArr1[i] + iArr2[i];
24
}
25
flag = "";
26
if (iTemp > 9999)
{
27
iTemp = iTemp - 10000;
28
flag = "add";
29
}
30
31
iArr3[i] = iTemp;
32
}
33
String temp = "";
34
for (int i = 0; i < iArr3.length; i++)
{
35
36
if (iArr3[i] < 10)
{
37
temp = "000" + String.valueOf(iArr3[i])+temp;
38
39
}
40
else if (iArr3[i] < 100)
{
41
temp = "00" + String.valueOf(iArr3[i])+temp;
42
}
43
else if (iArr3[i] < 1000)
{
44
temp = "0" + String.valueOf(iArr3[i])+temp;
45
}
46
else
{
47
temp = String.valueOf(iArr3[i])+temp;
48
}
49
50
}
51
System.out.println(temp);
52
}
53
54
int[] arr(String s)
{
55
int j = s.length() / 4;
56
57
int[] ArrOne = new int[j + 1];
58
int iLength;
59
iLength = s.length();
60
61
for (int i = 0; i < j; i++)
62
63
{
64
65
ArrOne[i] = Integer.parseInt(s.substring(iLength - 4 * (i + 1),
66
iLength - 4 * i));
67
68
}
69
70
return ArrOne;
71
72
}
73
}
74
posted @
2005-08-22 12:31 jam 閱讀(567) |
評論 (0) |
編輯 收藏

2005年8月5日
摘要: EJB
1.EJB體系結構:
包括客戶端、服務器端。
客戶端:包含了調用EJB組件特定業務所需的EJB接口(包括本地和遠程EJB接口);服務器端處理對象的句柄。
服務器端:包含了EJB組件實現的實例;用來在客戶端...
閱讀全文
posted @
2005-08-05 09:25 jam 閱讀(2235) |
評論 (1) |
編輯 收藏

2005年8月2日
之前在網上大家給出的方法都是:
byte temp[] = str.getBytes("8859_1");
strResult = new String(temp, "UTF8");
但是經過我測試,發現轉換不正確。沒有正確轉換成UTF。
下面給出我的實現方法:
1
private OutputStreamWriter outputwriter = null;
2
3
outputwriter = new OutputStreamWriter(out, "UTF-8");
4
outputwriter.write(strValue, 0, strValue.length()); 這樣就ok了,測試方法,將轉換后的字符串,用ULtra打開,進行字符轉換UTF到ASCII如果不是亂碼就OK了!
posted @
2005-08-02 15:48 jam 閱讀(1650) |
評論 (0) |
編輯 收藏
用Java寫properties文件時,如果直接用setProperties和store方法往FileOutputStream寫,寫出來的東西面目全非了。沒有正確的格式。因此我使用了這個方法來寫,一行一行的讀。一行一行地寫。
1
/**//**
2
* write property.
3
* @param title parameter defined in properties file
4
* @param key parameter defined title value
5
* @return String return value
6
*/
7
String writeProp(String filePath, String fileName, String title, String key,
8
Logger logger)
{
9
10
String strResult = "";
11
String pathAddFile = ""; //write file with path and name
12
String tempFile = "";
13
String strTemp = ""; //use for identify if the modify is success
14
//filePath is null the file in the default path ,else file in the filePath+\+fileName
15
if (filePath.equals(""))
{
16
pathAddFile = fileName;
17
tempFile = "temp.properties";
18
}
19
else
{
20
pathAddFile = filePath + systemSeparator + fileName;
21
tempFile = filePath + systemSeparator + "temp.properties";
22
}
23
//properties file
24
File aFile = new File(pathAddFile);
25
//temp file
26
File tFile = new File(tempFile);
27
if (!aFile.exists())
{
31
strResult = "error";
32
return strResult;
33
}
34
//set property to properties
35
try
{
36
FileReader fr = new FileReader(pathAddFile);
37
38
BufferedReader br = new BufferedReader(fr);
39
try
{
40
FileWriter fw = new FileWriter(tempFile);
41
PrintWriter out = new PrintWriter(fw);
42
43
String strLine = br.readLine().trim();
44
while (strLine != null)
{
45
//identify if strLine have title,have change key
46
if (strLine.startsWith(title))
{
47
strLine = title + "=" + key;
48
strTemp = "1";
49
}
50
out.write(strLine);
51
out.println();
52
out.flush();
53
//read next line
54
strLine = br.readLine();
55
}
56
fw.close();
57
out.close();
58
//close BufferedReader object
59
br.close();
60
//close file
61
fr.close();
62
//delete properties file
63
if (aFile.exists())
{
64
if (!aFile.delete())
{
68
return "error";
69
}
70
}
71
//rename temp file to properties file
72
if (!tFile.exists())
{
76
return "error";
77
}
78
tFile.renameTo(aFile);
79
if (!strTemp.equals("1"))
{
80
//there is no title prop exit so modify failed
85
strResult = "error";
86
87
}
88
return strResult;
89
}
90
catch (IOException ex2)
{
91
ex2.printStackTrace();
92
strResult = "error";
93
logger.fatal(
94
"CmnEToyotaExtractProp ----- writeProp ----- failed !");
95
return strResult;
96
97
}
98
}
99
catch (FileNotFoundException ex1)
{
100
ex1.printStackTrace();
103
strResult = "error";
104
return strResult;
105
}
106
107
} 該方法實現了對屬性文件參數的修改。
posted @
2005-08-02 15:41 jam 閱讀(5182) |
評論 (0) |
編輯 收藏
但愿這次我會堅持下去,寫點東西,幫助年輕的同志。
posted @
2005-08-02 15:34 jam 閱讀(243) |
評論 (0) |
編輯 收藏