How to Export Preferences to XML File

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - How to Export Preferences to XML File

This Java example program shows how to export Preferences to an XML file.

[edit] Java Source Code

  • Package: example.preferenceproperties6
  • File: PreferenceExample.java
package example.preferenceproperties;
/**
 * @author Code Panda
 * Copyright (C) 2008 Java ExampleShow (http://java.exampleshow.com)
 *
 * This code is free software; you can redistribute it and/or modify it.
 * However, this header must remain intact and unchanged. Additional
 * information may be appended after this header. Publications based on
 * this code must also include an appropriate reference.
 * 
 * This code is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE.
 *
 */
 
import java.io.FileOutputStream;
import java.util.prefs.Preferences;
 
public class PreferenceExport {
    public static void main(String args[]) throws Exception {
	Preferences prefsRoot = Preferences.userRoot();
	Preferences myPrefs = prefsRoot.node("preferenceExportExample");
 
	myPrefs.put("name", "pet");
	myPrefs.put("age", "3");
	myPrefs.put("gender", "female");
 
	FileOutputStream fos = new FileOutputStream("C:/exampleshow/preference.xml");
 
	myPrefs.exportSubtree(fos);
	fos.close();
 
    }
}

[edit] What Result You Can Get

In the folder c:\exampleshow, you will get a xml file preference.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE preferences SYSTEM "http://java.sun.com/dtd/preferences.dtd">
<preferences EXTERNAL_XML_VERSION="1.0">
  <root type="user">
    <map/>
    <node name="preferenceExportExample">
      <map>
        <entry key="name" value="pet"/>
        <entry key="age" value="3"/>
        <entry key="gender" value="female"/>
      </map>
    </node>
  </root>
</preferences>

[edit] Required External Libraries and/or Files for this Java Example

Need nothing.


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools