简体   繁体   English

如何从XML属性文件加载变量?

[英]How can I load variables from an XML properties file?

I am currently loading properties from an XML file in Ant. 我目前正在从Ant中的XML文件加载属性。 However, I'd like to perform my current ant task inside a for loop while loading a new XML properties file for the same set of properties each time. 但是,我想在for循环中执行当前的ant任务,同时每次都为相同的一组属性加载新的XML属性文件。

I'm aware of the existence of ant-contrib's var task which allows me to override properties. 我知道存在ant-contrib的var任务,该任务使我可以覆盖属性。 However, I'm unsure of how to combine this with loading properties from an XML file. 但是,我不确定如何将其与从XML文件加载属性结合使用。 I thought at first about iterating through all of the properties and then setting them to new values using the propertyregex task. 我首先考虑过遍历所有属性,然后使用propertyregex任务将它们设置为新值。 Unfortunately, as I started writing code to do that I realized I didn't know how to actually load the property values still, since they can't overwrite previously set properties. 不幸的是,当我开始编写代码来实现此目的时,我意识到我仍然不知道如何实际加载属性值,因为它们无法覆盖先前设置的属性。 (Well, they can using the var task, but this can't be used to load from an XML file as far as I can tell.) (好吧,他们可以使用var任务,但是就我所知,这不能用于从XML文件加载。)

Any ideas? 有任何想法吗? What I'm ideally looking for is a task that would be called something like 我理想中正在寻找的任务将被称为

<xmlvars file="myxmlpropertyfile.xml"/>

which would function just like 就像

<xmlproperty file="myxmlpropertyfile.xml"/>

with the exception that it overwrites the variables. 除了它会覆盖变量之外。

One of the things you can do with the <xmlproperty> task is to prefix each property with a specific value. <xmlproperty>任务可以做的一件事是为每个属性添加一个特定的值。 Why not simply use the name of your parameter as a prefix? 为什么不简单地使用参数名称作为前缀呢?

Otherwise, you could use the <echoproperties> task to unset all of the properties, and then loop through the next iteration of your <for> task. 否则,您可以使用<echoproperties>任务取消设置所有属性,然后循环遍历<for>任务的下一个迭代。

Something like this, but this isn't tested: 这样的东西,但是没有经过测试:

 <for param="my.directory">
     <fileset dir="${some.directory}"/>
     <sequential>
         <xmlproperty file="@{my.directory}/myxmlpropertyfile.xml"
            prefix="foo-fighters"/>
          <blah, blah, blah/>
          <for param="reset.var">
              <echoproperty prefix=foo-fighters"/>
              <sequential>
                  <var name="@{reset.var}"
                     unset="true"/>
              </sequential>
          </for>
     </sequential>
 </for>

Basically, you use the outer <for> loop to loop through your directories, and use <xmlproperty> to set the property names. 基本上,您使用外部<for>循环遍历目录,并使用<xmlproperty>设置属性名称。 The <blah blah blah/> means do what you want. <blah blah blah/>意味着要做你想做的。 Then, before you go to the next iteration of your outer <for> loop, you do an inner <for> loop that will unset all the variables you previously set in the <xmlproperty> task. 然后,在转到外部<for>循环的下一个迭代之前,先执行内部 <for>循环,该循环将取消设置先前在<xmlproperty>任务中设置的所有变量。

The trick is using a variable prefix that will guarantee that the variable names are easily found. 诀窍是使用变量前缀,以确保容易找到变量名。 Thus foo-fighters . 因此, foo-fighters

It looks like I should be able to accomplish this by using the var task's unset attribute. 看来我应该能够通过使用var任务的unset属性来完成此任务。 Will report back... 会回报...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从属性文件中指定的XML文件加载Spring bean定义? - How do I load Spring bean definitions from XML file specified in a properties file? 从属性文件加载 static 变量值 - Load static variables values from properties file 如何将变量添加到xml属性文件? - How to add variables to xml properties file? 如何将.properties文件更改为XML文件? - How can i change a .properties file into an XML file? 在 Java 中,使用 java.nio 库和 FileChannel,如何从文件加载 Properties 对象? - In Java, using the java.nio library and a FileChannel, how can I load a Properties object from a file? 如何在Tomcat 8中创建新的共享文件夹并从中加载我的属性文件 - How can I create a new shared folder in Tomcat 8 and load my properties file from that 无法加载包装器属性,属性文件包含来自 xml 的代码 - Could not load wrapper properties, properties file contains code from xml 如何更改cfg.xml文件的属性? - how can I change the properties of the cfg.xml file? 如何从可执行的Jar文件中加载xml资源文件并将其保存到jar所在的文件夹中? - How can I load an xml resource file from within an executible Jar file and save it to the folder the jar is located in? 如何从数据库加载Blueprint XML的属性 - How to load properties for Blueprint XML from database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM