26: *# FileApp
27: *log4j.appender.FileApp=org.apache.log4j.RollingFileAppender
28: *log4j.appender.FileApp.File=./log4e.log
29: *log4j.appender.FileApp.MaxFileSize=500KB
30: *# Keep one backup file
31: *log4j.appender.FileApp.MaxBackupIndex=1
32: *log4j.appender.FileApp.layout=org.apache.log4j.PatternLayout
33: *log4j.appender.FileApp.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
34: */
35:
36:
37: /*應用此XML文件做測試
38: *
39: *
40: *
41: *zhangzhiyun@hp
42: *111111
43: *6.01.06.00
44: *
45: */
46: package xml;
47:
48: import java.io.BufferedReader;
49: import java.io.BufferedWriter;
50: import java.io.File;
51: import java.io.FileReader;
52: import java.io.FileWriter;
53: import java.io.IOException;
54:
55: import org.apache.log4j.Logger;
56: import org.dom4j.Attribute;
57: import org.dom4j.DocumentException;
58: import org.dom4j.DocumentHelper;
59: import org.dom4j.Element;
60:
61: /**
62: * @author julysea
63: *
64: * 一個用Dom4j解析xml的例子
65: *
66: */
67: public class Dom4jTest {
68:
69: private static final Logger logger = Logger.getLogger(Dom4jTest.class);
70:
71: public static void main(String[] args) throws IOException,
72: DocumentException {
73: BufferedReader reader=new BufferedReader(new FileReader("ew.xml"));
74: String tempStr;
75: String ewXml="";
76: while((tempStr=reader.readLine())!=null) {
77: ewXml=ewXml+tempStr;
78: logger.debug(tempStr);
79: }
80: Element root = null;
81:
82: root = DocumentHelper.parseText(ewXml).getRootElement();
83: Attribute rootCmd=root.attribute("cmd");
84: Attribute rootVersion=root.attribute("version");
85: logger.debug("rootNmae = "+root.getName());
86: logger.debug("EW'cmd = "+rootCmd.getValue());
87: logger.debug("EW'version = "+rootVersion.getValue());
88:
89: Element usrName=root.element("Username");
90: logger.debug("EW.Username value = "+usrName.getTextTrim());
91:
92: Element source=root.element("Source");
93: Attribute sourceUns=source.attribute("uns");
94: logger.debug("EW.Source'uns"+sourceUns.getValue());
95: Attribute sourceType=source.attribute("type");
96: logger.debug("EW.Source'type = "+sourceType.getValue());
97:
98:
99: //創(chuàng)建一個Xml文件
100: Element user=DocumentHelper.createElement("User");
101: user.addAttribute("type", "user");
102: user.addElement("name").addAttribute("type", "PinYin").setText("Julysea");
103: user.addElement("age").setText("29");
104: String oneXml=user.asXML();
105:
106: BufferedWriter out=new BufferedWriter(new FileWriter("oneXml.xml"));
107: out.write(oneXml);
108: out.close();
109: }
110: }
Dom4j編碼問題徹底解決
相關推薦:北京 | 天津 | 上海 | 江蘇 | 山東 |
安徽 | 浙江 | 江西 | 福建 | 深圳 |
廣東 | 河北 | 湖南 | 廣西 | 河南 |
海南 | 湖北 | 四川 | 重慶 | 云南 |
貴州 | 西藏 | 新疆 | 陜西 | 山西 |
寧夏 | 甘肅 | 青海 | 遼寧 | 吉林 |
黑龍江 | 內蒙古 |