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 }
订阅:
博文评论 (Atom)
没有评论:
发表评论