开发者

loading xml from properties file

开发者 https://www.devze.com 2023-02-08 17:05 出处:网络
is it possible to load data from a properties file to xml file using spring? if yes can anyone give an example of that?

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=***


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.


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.


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:

  1. read the XML and extract the details that you need.
  2. load the properties
  3. merge the details from the properties into the details.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消