Skip to content

Commit 6f222e7

Browse files
committed
feat(ReadXMLUsingPropertieClass): add demo for reading XML configuration with Properties
What - Added ReadXMLUsingPropertieClass in Package2. - Uses java.util.Properties to load key-value pairs from an XML file. - Prints entire Properties object and retrieves value for key `"Name: "`. Why - Demonstrates how to load configuration stored in XML format. - Complements previous examples using `.properties` files and `storeToXML()`. - Useful when config needs structured storage or integration with XML tools. How - Created new Properties object. - Called loadFromXML(FileInputStream) to parse XML file into Properties. • XML must follow the DTD defined by Properties (keys/values as `<entry>` elements). - Accessed individual property using getProperty("Name: "). - Printed full Properties map to console. Logic - loadFromXML reads standard Java Properties XML format: <properties> <comment>...</comment> <entry key="Name: ">Value</entry> </properties> - Keys and values remain Strings. - If the key is not found, getProperty() returns null. - Unlike `.properties` files, encoding is always UTF-8 for XML. Real-life applications - Store user preferences, metadata, or app settings in XML form. - Easier integration with XML-based configuration systems. - Better readability and extensibility compared to flat `.properties` files. - Common in apps where configuration must be both human-readable and tool-friendly. Summary - ReadXMLUsingPropertieClass shows loading settings from an XML file into Properties. - Demonstrates retrieving both all entries and a specific key. - Highlights Properties versatility: supports both `.properties` (text) and `.xml` formats. Signed-off-by: https://github.com/Someshdiwan <someshdiwan369@gmail.com>
1 parent 3096d24 commit 6f222e7

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package Package2;
2+
3+
import java.io.FileInputStream;
4+
import java.util.Properties;
5+
6+
public class ReadXMLUsingPropertieClass {
7+
public static void main(String[] args)throws Exception {
8+
Properties p = new Properties();
9+
p.loadFromXML(new FileInputStream("/Users/somesh/Java SE/JavaEvolution-Learning-Growing-Mastering/Section23JavaIOStreams/Properties/src/Package2/Person.xml"));
10+
11+
System.out.println(p);
12+
System.out.println(p.getProperty("Name: "));
13+
}
14+
}

0 commit comments

Comments
 (0)