How to convert XML file into properties file - Java XML Tutorial



In the last article, we show you how to convert properties file into an XML file. See following XML file :

1. INPUT: user.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"><properties><comment>user detail</comment><entry key="email">admin@interviewbubble.com</entry><entry key="username">admin</entry></properties>

2. Function Details:

Loads all of the properties represented by the XML document on the specified input stream into this properties table.

public void loadFromXML(InputStream in)                 throws IOException,                        InvalidPropertiesFormatException

3. SOURCE CODE:

package com.example.TestApp;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.util.Properties;public class XMLFile2PropertieFile { public static void main(String[] args) throws IOException {           InputStream os = new FileInputStream("/Users/admin/Desktop/user.xml");    Properties properties = new Properties();    properties.loadFromXML(os);    String username = properties.getProperty("username");    String email = properties.getProperty("email");    System.out.println("username: " + username + "\n" + "email: " + email); }}

4. OUTPUT:

username: adminemail: admin@interviewbubble.com