Basically I saw some kid posting on how to load data through .txt files, so I thought I'd post how to do it through XML.
This has probably been posted before but I haven't seen anything, so don't flame me.
Okay, first of all - the methods. Add these anywheres, I don't care.
Code:
public void LoadProperties(String property1, String property2) {
LoadProperties lp = new LoadProperties();
try {
Properties properties = lp.readXmlProperties("yourfile.xml");
properties.list(System.out);
String data1 = properties.getProperty(property1);
System.out.println(data1);
String data2 = properties.getProperty(property2);
System.out.println(data2);
} catch (Exception e) {
e.printStackTrace();
}
}
public Properties readXmlProperties(String file) throws Exception {
Properties properties = new Properties();
FileInputStream f = new FileInputStream(file);
properties.loadFromXML(f);
return properties;
}
Usage:
Code:
LoadProperties("data1", "data2");
Also don't forget to fill out the "yourfile" part.
Basically just read through that, you will get the hang of it. Now here is the XML file.
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Comment.</comment>
<entry key="data1">Data1 Entry</entry>
<entry key="data2">Data2 Entry</entry>
</properties>