How to load Properties from XML file in java?

Below java code example shows how to load properties from XML file –

/****************************************************************************************
 * Created on 08-2011 Copyright(c) https://kodehelp.com All Rights Reserved.
 ****************************************************************************************/
package com.kodehelp.java.io;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

/**
 * Created by Kodehelp.com.
 * User: https://kodehelp.com
 * Date: Aug 1, 2011
 */
public class LoadPropertiesFromXML {
    public static void main(String args[]){
        Properties prop = new Properties();
        try {
            FileInputStream fileInputStream = new FileInputStream("/config.xml");
            prop.loadFromXML(fileInputStream);
            prop.list(System.out);
            String userName = prop.getProperty("USERNAME");
            System.out.println("USERNAME ::"+userName);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (InvalidPropertiesFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
<!--?xml version="1.0" encoding="UTF-8"?-->

Robert
 secret

You can also check example for loading properties from text file in java

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.