How to convert properties file into XML file – Java
Published: August 23, 2010 , Updated: August 4, 2011 , Author: mkyong
Many developers may not aware of this function, actually, the java.util.Properties class come with a storeToXML() method to convert existing properties data into a XML file.
Note
Please refer to this Properties JavaDoc for detail explanation.
Please refer to this Properties JavaDoc for detail explanation.
import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Properties; public class PropertiesXMLExample { public static void main(String[] args) throws IOException { Properties props = new Properties(); props.setProperty("email.support", "donot-spam-me@nospam.com"); //where to store? OutputStream os = new FileOutputStream("c:/email-configuration.xml"); //store the properties detail into a pre-defined XML file props.storeToXML(os, "Support Email","UTF-8"); System.out.println("Done"); } }
The above example will write the properties detail into a XML file “c:/email-configuration.xml“.
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>Support Email</comment> <entry key="email.support">donot-spam-me@nospam.com</entry> </properties>
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~
If my xml is of below,How can i write the above java code????
wrap your xml code properly.
hi,
Thank you for your very helpful site.
[...] last article, it shows how to store properties file into XML file, and generate the following XML file : <?xml version="1.0" [...]