Reputation: 3917
is it possible to load data from a properties file to xml file using spring? if yes can anyone give an example of that?
here is the xml:
<cluster balancer="load"
dialect="net.sf.hajdbc.dialect.MySQLDialect"
default-sync="full"
transaction-mode="parallel"
auto-activate-schedule="0 * * ? * *"
failure-detect-schedule="0 * * ? * *"
meta-data-cache="none">
<database id="database1">
<driver>***</driver>
<url>***</url>
<user>***</user>
<password>***</password>
</database>
here is the datasource
database.driver=***
database1.url=***
database1.username=***
database1.password=***
Upvotes: 1
Views: 3691
Reputation: 718718
is it possible to load data from a properties file to xml file without using spring?
Yes, but only if you write a whole bunch of Java code to:
I'm not aware of any generic solution to this problem apart from Spring.
Actually, I'm not sure that you can do exactly this using Spring either.
Spring DI wiring has mechanisms for substituting complete bean property values (PropertyOverrideConfigurer
) or placeholders embedded in property values (PropertyPlaceholderConfigurer
). However, POC and PPC operate on the values of bean properties declared in Spring bean definitions. What you have in your example looks like plain XML ... not Spring bean declarations.
Upvotes: 0
Reputation: 2641
You don't need Spring for that. What you have to do is extract the properties using getProperty(). An example follows:
File propertiesFile = ...
Properties properties = new Properties();
FileInputStream fis = new FileInputStream(propertiesFile);
properties.load(fis);
String databaseDriver = properties.getProperty("database.driver");
String database1Url = properties.getProperty("database1.url");
String database1Username = properties.getProperty("database1.username");
(I left out exceptions for the sake of simplicity)
and use the Java API for XML to create your XML file. An example how to begin creating XML follows:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element rootElement = document.createElement("database");
document.appendChild(rootElement);
...
google "java create xml" or similar to find out how to create the xml according to your needs.
Upvotes: 3
Reputation: 3347
If it is a Spring properties file, it is already XML. You can read this with the standard Java XML parsing libraries if you are so inclined. http://www.mkyong.com/tutorials/java-xml-tutorials/ has several turorials on various methods to do it.
Upvotes: 0