Section 9.2. Loading Properties from XML Files


9.2. Loading Properties from XML Files

The xmlproperty task loads property values from a well-formed XML file. The way you structure the XML document determines the names of the properties it creates.

Say you have this XML document, properties.xml, that defines a property named name1, and two nested properties, firstName and lastName, creating the properties name1.firstName and name1.lastName:

<name1 language="english">     <firstName language="german">Stefan</firstName>     <lastName>Edwards</lastName> </name1>

You can load these properties into a build with the xmlproperty task, as shown in Example 9-3. This build file displays the values of the properties created by properties.xml, name1.firstName, and name1.lastName. This example defines attributes for the name1 and firstName properties, creating an attribute named language which can be accessed as name1(language) and as name1.firstName(language). The build file displays the values of these properties.

Example 9-3. Loading XML properties (ch09/xmlproperty/build.xml)
<?xml version="1.0" ?> <project default="main">    <xmlproperty file="properties.xml" />     <target name="main">         <echo>             ${name1(language)}             ${name1.firstName}             ${name1.firstName(language)}             ${name1.lastName}         </echo>     </target>    </project>

Here's what the build file displays when run:

%ant Buildfile: build.xml main:      [echo]      [echo]             english      [echo]             Stefan      [echo]             german      [echo]             Edward      [echo] BUILD SUCCESSFUL Total time: 0 seconds

The attributes of this task appear in Table 9-5.

Table 9-5. The xmlproperty task's attributes

Attribute

Description

Required

Default

collapseAttributes

Specifies you want to treat attributes as nested elements

No

false

file

Specifies the XML file you want to parse for properties

Yes

 

includeSemanticAttribute

Specifies you want to include the semantic attribute name as part of the property name

No

false

keepRoot

Specifies you want to make the XML root tag the first value in the property name

No

true

prefix

Specifies the prefix to prepend to each property automatically

No

 

rootDirectory

Specifies the root directory to search for files

No

${basedir}

semanticAttributes

Specifies you want to use semantic handling of attribute names

No

false

validate

Specifies you want to validate the input file

No

false




    Ant. The Definitive Guide
    Ant: The Definitive Guide, 2nd Edition
    ISBN: 0596006098
    EAN: 2147483647
    Year: 2003
    Pages: 115
    Authors: Steve Holzner

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net