1

I would like to read the values from external properties file into XML. Say :i have firstname and lastname in properties file and i would like to read them in my XML file.

tried with doctype but no luck

<!DOCTYPE project SYSTEM "">
<project name="My Project" default="D:/Apache24/htdocs/">
<property file="build.properties"/>
      <!-- set global properties -->
      <property name="FirstName" value="first"/>
      <property name="LastName" value="last"/>
         <echo message = "First name is = ${FirstName}"/><echo message ="Last name is ${Last Name}"/></target>
   </project>

Same XML is shown upon executing

Driod
  • 23
  • 6

1 Answers1

0

The closest you can get using just XML without using further XML processing tools is using entities, XML's mechanism for text substitution variables:

<!DOCTYPE project [
  <!ENTITY FirstName "first">
  <!ENTITY LastName "last">
]>
<project name="My Project">
  <property name="FirstName" value="&first;"/>
  <property name="LastName" value="&last;"/>
</project>

You can store your entities in an external file as well:

<!-- myproperties.dtd -->
<!ENTITY FirstName "first">
<!ENTITY LastName "last">

then include it into your main file as follows:

<!DOCTYPE project SYSTEM "myproperties.dtd">
<project name="My Project">
  <property name="FirstName" value="&first;"/>
  <property name="LastName" value="&last;"/>
</project>

You can also further organize your external myproperties.dtd file (or whatever name you chose) by having it include a common globalproperties.dtd or so and then declare only project-specific entities, or overides global properties/entities, etc.

See also XML configuration inheritance, avoid duplications.

SGML (on which XML is based) has "short references" which you can use to actually parse a properties file (or many other types of custom text file formats) with the usual syntax eg. lines containing item=value pairs. But you wouldn't be able to assign values to entities using that technique as short references (= custom character sequences that SGML replaces by tags or other text) are only recognized in content rather than in DOCTYPE declaration.

imhotap
  • 2,275
  • 1
  • 8
  • 16
  • As i stated in my description i have used the DOCTYPE content, but am unable to append/assign the values dynamically to the XML (Say: firstname and lastname from command line etc) those values needs to be append it to the firstname and lastname xml parameters. – Driod Jun 17 '19 at 06:01
  • I have created two one .xml and .properties file and tried to read the first,middle & last name from properties file using bean concept. find the code below. – Driod Jun 18 '19 at 05:50
  • D:/Apache24/htdocs/ok.properties – Driod Jun 18 '19 at 05:50
  • @Driod Sorry that is Spring-specific property syntax (has nothing to do with bare XML); I can't help you here – imhotap Jun 18 '19 at 16:35