How to convert XML file into properties data in Java

  • 04 April 2017
  • ADM

 

How to convert XML file into properties data in Java - images/logos/java.jpg

 

The java.util.Properties class come with a loadFromXML() method to convert an XML file to properties data. To see how to convert from Properties object to an XML file, check How to convert properties file into XML file in Java.

Example

package com.admfactory.basic;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;

public class XMLToProperties {

    public static void main(String[] args) throws Exception {
	System.out.println("Properties to XML Example");
	System.out.println();

	Properties props = new Properties();

	InputStream is = new FileInputStream("d:/admfactory.com/config.xml");

	/** load the xml file into properties format */
	props.loadFromXML(is);

	String username = props.getProperty("username");
	String email = props.getProperty("email");

	System.out.println("username: " + username);
	System.out.println("email: " + email);
    }
}

Output

Properties to XML Example

username: admin
email: admin@admfactory.com

 

References