一、测试用的em.xml
<?xml version="1.0" encoding="GB2312"?>
<EW cmd="login" mod="Login" version="6.0">
<Source uns="" type="user"/>
<Username>zhangzhiyun@hp</Username>
<Password>111111</Password>
<Version>6.01.06.00</Version>
</EW>
二、Dom4jTest.java
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import org.apache.log4j.Logger; import org.dom4j.Attribute; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; /** * @author julysea * * 一个用Dom4j解析xml的例子 * */ public class Dom4jTest { private static final Logger logger = Logger.getLogger(Dom4jTest.class); public static void main(String[] args) throws IOException, DocumentException { BufferedReader reader=new BufferedReader(new FileReader("ew.xml")); String tempStr; String ewXml=""; while((tempStr=reader.readLine())!=null) { ewXml=ewXml+tempStr; logger.debug(tempStr); } Element root = null; root = DocumentHelper.parseText(ewXml).getRootElement(); Attribute rootCmd=root.attribute("cmd"); Attribute rootVersion=root.attribute("version"); logger.debug("rootNmae = "+root.getName()); logger.debug("EW'cmd = "+rootCmd.getValue()); logger.debug("EW'version = "+rootVersion.getValue()); Element usrName=root.element("Username"); logger.debug("EW.Username value = "+usrName.getTextTrim()); Element source=root.element("Source"); Attribute sourceUns=source.attribute("uns"); logger.debug("EW.Source'uns="+sourceUns.getValue()); Attribute sourceType=source.attribute("type"); logger.debug("EW.Source'type = "+sourceType.getValue()); //创建一个Xml文件 Element user=DocumentHelper.createElement("User"); user.addAttribute("type", "user"); user.addElement("name").addAttribute("type", "PinYin").setText("Julysea"); user.addElement("age").setText("29"); String oneXml=user.asXML(); BufferedWriter out=new BufferedWriter(new FileWriter("oneXml.xml")); out.write(oneXml); out.close(); } }
三、运行:将log4j.properties文件与Dom4jTest.java放同一目录下编译运行,以下是输出结果:
log4e.log文件内容:
2006-04-04 09:16:13,363 [main] DEBUG Dom4jTest - <?xml version="1.0" encoding="GB2312"?>
2006-04-04 09:16:13,373 [main] DEBUG Dom4jTest - <EW cmd="login" mod="Login" version="6.0">
2006-04-04 09:16:13,373 [main] DEBUG Dom4jTest - <Source uns="" type="user"/>
2006-04-04 09:16:13,373 [main] DEBUG Dom4jTest - <Username>zhangzhiyun@hp</Username>
2006-04-04 09:16:13,373 [main] DEBUG Dom4jTest - <Password>111111</Password>
2006-04-04 09:16:13,373 [main] DEBUG Dom4jTest - <Version>6.01.06.00</Version>
2006-04-04 09:16:13,373 [main] DEBUG Dom4jTest - </EW>
2006-04-04 09:16:13,594 [main] DEBUG Dom4jTest - rootNmae = EW
2006-04-04 09:16:13,594 [main] DEBUG Dom4jTest - EW'cmd = login
2006-04-04 09:16:13,594 [main] DEBUG Dom4jTest - EW'version = 6.0
2006-04-04 09:16:13,594 [main] DEBUG Dom4jTest - EW.Username value = zhangzhiyun@hp
2006-04-04 09:16:13,594 [main] DEBUG Dom4jTest - EW.Source'uns=
2006-04-04 09:16:13,594 [main] DEBUG Dom4jTest - EW.Source'type = user
oneXml.xml的内容:
<User type="user"><name type="PinYin">Julysea</name><age>29</age></User>
四、log4j日志配置文件:log4j.properties
log4j.rootCategory=ConApp, FileApp
# ConApp is set to be a ConsoleAppender.
log4j.appender.ConApp=org.apache.log4j.ConsoleAppender
log4j.appender.ConApp.Target=System.out
log4j.appender.ConApp.layout=org.apache.log4j.PatternLayout
log4j.appender.ConApp.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
# FileApp
log4j.appender.FileApp=org.apache.log4j.RollingFileAppender
log4j.appender.FileApp.File=./log4e.log
log4j.appender.FileApp.MaxFileSize=500KB
# Keep one backup file
log4j.appender.FileApp.MaxBackupIndex=1
log4j.appender.FileApp.layout=org.apache.log4j.PatternLayout
log4j.appender.FileApp.layout.ConversionPattern=%d [%t] %-5p %c - %m%n