Using Properties class:
favoriteSeason: spring
favoriteDay: today
favoriteFruit: apple
prop.xml - file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Here are some favorites</comment>
<entry key="favoriteSeason">spring</entry>
<entry key="favoriteFruit">apple</entry>
<entry key="favoriteDay">today</entry>
</properties>
- import java.io.File;
- import java.io.FileInputStream;
- import java.util.Enumeration;
- import java.util.Properties;
- public class ReadPropertiesXmlFile {
- public static void main(String[] args) throws Exception {
- File file = new File("D:\\prop.xml");
- FileInputStream fileInput = new FileInputStream(file);
- Properties properties = new Properties();
- properties.loadFromXML(fileInput);
- fileInput.close();
- Enumeration enuKeys = properties.keys();
- while (enuKeys.hasMoreElements()) {
- String key = (String) enuKeys.nextElement();
- String value = properties.getProperty(key);
- System.out.println(key + ": " + value);
- }
- }
- }
favoriteSeason: spring
favoriteDay: today
favoriteFruit: apple
prop.xml - file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Here are some favorites</comment>
<entry key="favoriteSeason">spring</entry>
<entry key="favoriteFruit">apple</entry>
<entry key="favoriteDay">today</entry>
</properties>
No comments:
Post a Comment