An example of applying dom4j

Source: Internet
Author: User
Dom
Created with Colorer-take5 Library. Type ' net.sf.colorer.filetype@777255 ' 0:/* 1: * Created on 2005-4-19 2: * 3: * Copyright (c) Julysea 4: * window-p References-java-code Style-code Templates 5: * * 6:7: * Apply this log4j log4j.properties configuration file 8: * 9: *##################### ##################################################: *# Categories and Levels 11: *################################ #######################################: *log4j.rootcategory=debug, Fileapp, Conapp 14: * Log4j.category.de.jayefem=debug, Fileapp, Conapp 15: * 16: *######################################################## ###############: *# appenders 18: *####################################################################### 19: * 20 : *# Conapp is set to be a consoleappender. *log4j.appender.conapp=org.apache.log4j.consoleappender: *log4j.appender.conapp.target=system.out 23: * 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 4j.appender.fileapp.file=./log4e.log: *log4j.appender.fileapp.maxfilesize=500kb *# Keep One backup File 31: * Log4j.appender.fileapp.maxbackupindex=1: *log4j.appender.fileapp.layout=org.apache.log4j.patternlayout 33: * log4j.appender.fileapp.layout.conversionpattern=%d [%t]%-5p%c-%m%n 34: * * 35:36:37:/* Apply this XML file to do the test. *: *<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> 44: * </EW>: * * 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:/**: * @author Julysea 63: * 64: * An example of XML parsing with dom4j: *: * * 67:public class Dom4jtest {68:69:private s Tatic final Logger Logger = Logger.getlogger (Dom4jtest.class); 70:71:public static void Main (string[] args) throws IOException, 72:documentexception {73:bufferedreader reader=new B Ufferedreader (New FileReader ("Ew.xml")); 74:string TempStr; 75:string ewxml= ""; 76:while ((Tempstr=reader.readline ())!=null) {77:ewxml=ewxml+tempstr; 78:logger.debug (TEMPSTR);: 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://Create an XML file 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 ("a"); 104:string onexml=user.asxml (); 105:106:bufferedwriter out=new BufferedWriter (New FileWriter (" Onexml.xml ")); 107:out.write (onexml); 108:out.close (); 109:}110:}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.