2010年6月30日星期三
用dom4j生成xml
01 import java.io.File;
02 import java.io.FileOutputStream;
03 import java.io.IOException;
04
05 import org.dom4j.Document;
06 import org.dom4j.DocumentHelper;
07 import org.dom4j.Element;
08 import org.dom4j.io.OutputFormat;
09 import org.dom4j.io.XMLWriter;
10
11
12 public class SoaToXML {
13
14
15 public static void execute(int userType,int bianhao){
16 Document document = DocumentHelper.createDocument();
17 document.setXMLEncoding("utf-8");
18 Element root = document.addElement("condition");
19 root.addElement("type").setText(String.valueOf(userType));
20 root.addElement("bianhao").setText(String.valueOf(bianhao));
21 String text = document.asXML();
22 printTest(text);
23 String path="soa_to_xml.xml";
24 if(writeFile(document, path)){
25 printTest("生成xml成功");
26 }else{
27 printTest("生成xml出错");
28 }
29 }
30 public static boolean writeFile(Document document, String fileName) {
31 try {
32
33 if (fileName == null || fileName.trim().equals("")) {
34 return false;
35 }
36 String path = "f:/" + fileName;
37
38 OutputFormat format = OutputFormat.createPrettyPrint();
39 XMLWriter output = new XMLWriter(new FileOutputStream(
40 new File(path)), format);
41
42
43 output.write(document);
44 output.close();
45 return true;
46 } catch (IOException e) {
47 e.printStackTrace();
48
49 return false;
50 }
51 }
52 public static void printTest(String content) {
53 System.out.println(content);
54
55 }
56 /**
57 * @param args
58 */
59 public static void main(String[] args) {
60 SoaToXML.execute(0, 1);
61
62 }
63
64 }
2010年6月11日星期五
java面试题
01 import java.util.*;
02 public class Test{
03 public static void main(String[] args){
04 new Child().m();
05 }
06 }
07 class Person{
08 public Person(){
09 System.out.println("Person");
10 m();
11 }
12 public void m(){
13 System.out.println("Person m");
14 }
15 }
16 class Child extends Person{
17 public Child(){
18 System.out.println("Child");
19 super.m();
20 }
21 public void m(){
22 System.out.println("Child m");
23 }
24 }
25
26 输出:
27 //Person
28 //Child m
29 //Child
30 //Person m
31 //Child m
知识点:构造方法调用顺序
方法重载
显式调用父类构造方法必须放在第一行
2010年6月10日星期四
jquery 验证所有text,textare非法字符
01 var $j = jQuery.noConflict();
02 //使用此方法验证所有text textarea 是否含有非法字符
03 $j(document).ready(function(){
04 $j("input[type='text']").each(function(i){
05 $j(this).keyup(function() { checkIllegalChar($j(this)); });
06 });
07 $j("textarea").each(function(i){
08 $j(this).keyup(function() { checkIllegalChar($j(this)); });
09 });
10 });
11
12
13
14 function checkIllegalChar(obj){
15 var value = obj.val();
16 if(!checkChar(value)){
17 obj.val(value.substring(0,value.length-1));
18 obj.focus();
19
20 }
21 }
22
23 //检查输入中的非法字符
24 function checkChar(InString) {
25 var RefString = "<";
26 var RefString2 = "%";
27 var RefString3 = "\"";
28 var RefString4 = ">";
29 var RefString5 = "~";
30 var RefString6 = "&";
31 var RefString7 = "?";
32 var RefString8 = "'";
33 for (Count = 0; Count < InString.length; Count++) {
34 TempChar = InString.substring(Count, Count + 1);
35 if ((RefString.indexOf(TempChar, 0) == 0) || (RefString2.indexOf(TempChar, 0) == 0) || (RefString3.indexOf(TempChar, 0) == 0) || (RefString4.indexOf(TempChar, 0) == 0) || (RefString5.indexOf(TempChar, 0) == 0) || (RefString6.indexOf(TempChar, 0) == 0) || (RefString7.indexOf(TempChar, 0) == 0) || (RefString8.indexOf(TempChar, 0) == 0)) {
36 alert("您的输入中含有非法字符\"<\",\"\"\",\"%\",\"> \",\"~\",\"&\",\"?\",\"'\",请重新输入!");
37 return (false);
38 }
39 }
40 return (true);
41 }
2010年6月4日星期五
订阅:
博文 (Atom)