An example of Dom4j application
Last Update:2017-01-13
Source: Internet
Author: User
Created with Colorer-take5 Library. type 'net. sf. colorer. fileType @ 777255 '0:/* 1: * Created on 2005-4-19 2: * 3: * Copyright (c) 2005 Julysea 4: * Window-Preferences-Java-Code Style-Code Templates 5: */6: 7:/* apply the log4j of log4j. properties configuration file 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 = o Rg. 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} % 5 p % 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 = 50 0KB 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] %-5 p % c-% m % n 34: */35: 36: 37:/* apply this XML file for testing 38: * 39: * <EW cmd = "login" mod = "Login" version = "6.0"> 40: * <Source uns = "" type = "user"/> 41: * <Username> zhangzhiyun @ hp </Username> 42: * <Password> 1111 11 </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. export enthelper; 59: import org. dom4j. element; 60: 61:/** 62: * @ author julysea 63: * 64: * an example of parsing xml with Dom4j 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: effecentexception {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 = incluenthelper. 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: // Create an Xml file 100: Element user = incluenthelper. 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 :}