Commons-configuration use

Source: Internet
Author: User

Introduced:

The Apache common-configuration tool can be used from Properties files, XML files, jndi,jdbc data sources, System Properties,applet Parameters,servlet parameters, etc. read the appropriate information.


Use steps:

Premise, the introduction of Commons-configuration-1.6.jar this jar package, but also must be reflected in the Commm-logging.jar,common-lang.jar and Common-collection.jar


Examples of XML operations:

Package com.dada.test;
Import Java.util.Arrays;

Import java.util.List;

Import org.apache.commons.configuration.XMLConfiguration;
	public class Xmlreadtest {public static void main (string[] args) {testreadxml ();
			The public static void Testreadxml () {try{xmlconfiguration config = new Xmlconfiguration ("Config.xml");
			For individual elements, the value String str = config.getstring ("Boy") can be obtained directly from the element name.
			
			System.out.println ("boy:\t" +str);
			For nested elements that occur in loops, you can get the collection value list<string> names = Config.getlist ("Student.name") by the parent element. Child elements.
			
			System.out.println ("Student.name:\t" +arrays.tostring (Names.toarray ()));
			There are multiple words for a single element containing a value such as: A,b,c,d can be obtained by fetching the collection list<string> titles = Config.getlist ("title");
			
			System.out.println ("Title:\t" +arrays.tostring (Titles.toarray ()));
			For the properties of a LABEL element, you can get the String size = config.getstring ("ball[@size]" by the label [@ property name];
			
			System.out.println ("ball[@size]:\t" +size); For nested tags, a String id = config.getstring can be obtained by means of a label name (index name) in order to obtain an item ("Student (1) [@id] ");
			
			System.out.println ("Student (1) [@id]:\t" +id);
			For the attribute name inside the tag, you can take String go = config.getstring ("Student (0). name[@go]");
			
			System.out.println ("Student (0). name[@go]:\t" +go);
			For the name of the property in the tag, you can also take the go = Config.getstring ("student.name (0) [@go]");
			
			System.out.println ("student.name (0) [@go]:\t" +go); /** output results are as follows: Boy:tom student.name: [Lily, Lucy] Title: [ABC, CBC, BBC, BBS] ball[@size]: Student ( 1) [@id]: 2 student (0). name[@go]: Common1 student.name (0) [@go]: Common1/} catch (Exception e) {E
		. Printstacktrace ();
 }
	} 
}


Config.xml file:


<?xml version= "1.0" encoding= "UTF-8"?>
<Students>
<boy>tom</boy>
<student Id= "1" >
<name go= "Common1" >lily</name>
<age>20</age>
</student>
<student id= "2" >
<name go= "Common2" >lucy</name>
<age>30</age>
</student>
<title>abc,cbc,bbc,bbs</title>
<ball size= ">pingpang</ball" >
</Students>


Properties Operation:

Package com.dada.test;
Import Java.util.Arrays;

Import java.util.List;
Import org.apache.commons.configuration.AbstractConfiguration;
Import org.apache.commons.configuration.Configuration;
Import org.apache.commons.configuration.ConfigurationException;

Import org.apache.commons.configuration.PropertiesConfiguration;
	public class Propertiesreadtest {public static void main (string[] args) {testreadproperties ();
		The public static void Testreadproperties () {//Note that the path defaults to the classpath root directory Configuration config;
			try {config = new propertiesconfiguration ("Config.properties");   
			For general properties directly fetched on line String ip=config.getstring ("IP");   
			
			int Port=config.getint ("port");   
			This property obtained from the above property is also a direct fetch of String title=config.getstring ("Application.title");   
			
			System.out.println (ip+ "\ n" +port+ "\ n" +title); To give a configuration more practical method, it is possible to read the configuration file, the value of the corresponding key value is null, then in the following method//You can set the default value for it. For example, the following example will be in config.properties this file to find the value of the ID, if the file does not have a configuration ID, the ID will be set to a value of 123//so that the JAva's wrapper class does not return a null value.   
			Although the function is very simple, but very convenient and practical. 
			String id=config.getstring ("id", "Defaultid");
			
			SYSTEM.OUT.PRINTLN (ID); If you have the following attribute keys=cn,com,org,uk,edu,jp,hk in the properties file, such a value contains more than one element value//So the following two methods can be string[] Keys1=config 
			. Getstringarray ("Keys");
			System.out.println (arrays.tostring (keys1));  
			List keys2=config.getlist ("Keys");
			
			System.out.println (Arrays.tostring (Keys2.toarray ())); The reason for the next two steps is that the default split symbol is a comma, while the value of con in the configuration file contains multiple dash delimiters, so you want to reset the separator read it out Abstractconfiguration.setdefaultlistdelimiter ('
			-');
			Config = new Propertiesconfiguration ("Config.properties");  
			List cons=config.getlist ("con");
			System.out.println (Arrays.tostring (Cons.toarray ())); /** * * The output is as follows * 127.0.0.1 * 8080 * killer App 1.6.2 * Defaulti d * [cn, com, org, UK, EDU, JP, HK] * [cn, com, org, UK, EDU, JP, HK] * [CN, com , org, UK, EDU, JP, HK] * * catch (ConfigurationexcePtion e) {e.printstacktrace ();
 }   
		
	}
}

Config.properties file:

ip=127.0.0.1
port=8080
sd=111
Application.Name = Killer App
Application.Version = 1.6.2
Application.title = ${application.name} ${application.version}
Keys=cn,com,org,uk,edu,jp,hk
Con=cn-com-org-uk-edu-jp-hk


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.