
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 阅读(1329) |
评论 (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 阅读(6801) |
评论 (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 阅读(562) |
评论 (0) |
编辑 收藏

2005年8月5日
摘要: EJB
1.EJB体系结构:
包括客户端、服务器端。
客户端:包含了调用EJB组件特定业务所需的EJB接口(包括本地和远程EJB接口);服务器端处理对象的句柄。
服务器端:包含了EJB组件实现的实例;用来在客户端...
阅读全文
posted @
2005-08-05 09:25 jam 阅读(2227) |
评论 (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 阅读(1645) |
评论 (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 阅读(5176) |
评论 (0) |
编辑 收藏
但愿这次我会坚持下去,写点东西,帮助年轻的同志。
posted @
2005-08-02 15:34 jam 阅读(241) |
评论 (0) |
编辑 收藏