본문 바로가기
프로그래밍/JAVA

[JAVA] XML 타입 프로퍼티 파일 처리

by 소나기_레드 2023. 2. 28.
반응형

1. 예제 소스

package test;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.Enumeration;

import java.util.Properties;

public class ReadPropertiesXmlFile {

    public static void main(String[] args) {

        try {

            File file = new File("test.xml");

            FileInputStream fileInput = new FileInputStream(file);

            Properties properties = new Properties();

            properties.loadFromXML(fileInput);

            fileInput.close();

            Enumeration enuKeys = properties.keys();

            while (enuKeys.hasMoreElements()) {

                String key = (String) enuKeys.nextElement();

                String value = properties.getProperty(key);

                System.out.println(key + ": " + value);

            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

}

2. 예제 XML

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

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

<properties>

<comment>Here are some favorites</comment>

    <entry key="favoriteSeason">summer</entry>

    <entry key="favoriteFruit">pomegranate</entry>

    <entry key="favoriteDay">today</entry>

</properties>

반응형

댓글