一个应用Dom4j的例子

80酷酷网    80kuku.com

  dom
Created with Colorer-take5 Library. Type 'net.sf.colorer.FileType777255' 0: /* 1: * Created on 2005-4-19 2: * 3: * Copyright (c) 2005 Julysea 4: * Window - Preferences - Java - Code Style - Code Templates 5: */ 6: 7: /*应用此log4j的log4j.properties配置文件 8: * 9: *####################################################################### 10: *# Categories and levels 11: *####################################################################### 12: * 13: *log4j.rootCategory=DEBUG, FileApp, ConApp 14: *log4j.category.de.jayefem=DEBUG, FileApp, ConApp 15: * 16: *####################################################################### 17: *# Appenders 18: *####################################################################### 19: * 20: *# ConApp is set to be a ConsoleAppender. 21: *log4j.appender.ConApp=org.apache.log4j.ConsoleAppender 22: *log4j.appender.ConApp.Target=System.out 23: *log4j.appender.ConApp.layout=org.apache.log4j.PatternLayout 24: *log4j.appender.ConApp.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n 25: * 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: *<EW cmd="login" mod="Login" version="6.0"> 40: *<Source uns="" type="user"/> 41: *<Username>zhangzhiyunhp</Username> 42: *<Password>111111</Password> 43: *<Version>6.01.06.00</Version> 44: *</EW> 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: //创建一个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: }



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: