How to get parameters from XML Properties File

Published on May 14, 2016 by Daniel Lanza

 java

Parameters are read from XML properties file and printed on console.

XML Properties File

The content of XML Properties File is the following:

<?xml version="1.0"?>
<properties>
    <name>Daniel</name>
    <lastName>Lanza</lastName>
    <age>29</age>
</properties>

And the file is located in the desktop:

User Java class

package com.dalanz.file;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class ReadPropertiesXML {

    private File fileSource;

    public ReadPropertiesXML (File fileSource) {
        this.fileSource = fileSource;
    }

    public String getProperty(String key) {

        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(this.fileSource);

            //Optional, but recommended
            //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
            doc.getDocumentElement().normalize();

            NodeList list = doc.getElementsByTagName(key);

            if (list.getLength() == 1)
                return list.item(0).getTextContent();
            else
                return null;

        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
}

Main Class

package test.com.dalanz;

import java.io.File;

import com.dalanz.file.*;

public class test {

    public static void main(String[] args) {

        File sourceFile = new File("/Users/dalanz/Desktop/person.xml");

        ReadPropertiesXML xml = new ReadPropertiesXML(sourceFile);

        System.out.println("Name: " + xml.getProperty("name"));
        System.out.println("Last Name: " + xml.getProperty("lastName"));
        System.out.println("Age: " + xml.getProperty("age"));
    }
}

Output

Name: Daniel
Last Name: Lanza
Age: 29
comments powered by Disqus

See also

How to get parameters from Java Properties File

Parameters are read from Java properties file and printed on console. Java Properties File The content of Java Properties File is the following: name = Daniel lastName = Lanza age = 29 And the file is located in the desktop: /Users/dalanz/Desktop/test.properties User Java class package com.dalanz.file; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class ReadProperties { private String fileSource; public ReadProperties (String fileSource) { this.fileSource = fileSource; } public String getProperty(String key) { Properties prop = new Properties(); InputStream input = null; try { input = new FileInputStream(this.

Read more

How to convert Java object to JSON and JSON to Java object with Gson

A simple Java object will become to JSON by using Gson. Besides, this will become into Java object. Maven dependencies <dependencies> <dependency> <groupId>com.google.code.gson/groupId> <artifactId>gson</artifactId> <version>2.6.2</version> </dependency> </dependencies> User Java class package com.dalanz.json; import java.util.ArrayList; import java.util.List; public class User { private String firstName; private String lastName; private int age; private List<String> messages; public User(){} public User(String firstName, String lastName, int age, List<String> messages) { this.firstName = firstName; this.lastName = lastName; this.

Read more

How to zip a folder and unzip a Zip file in Java

A folder with subfolders and files will be zipped. Besides, the zip file will be unzipped. Zip directory Java class package com.dalanz.file; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; public class ZipDirectory { private String folderPath; private String zipPath; private static ArrayList<String> fileList = null; private static String sourceFolder = null; public String getFolderPath() { return folderPath; } public void setFolderPath(String folderPath) { this.

Read more