Working with XML "templates"

  • Thread starter Michael Ransburg
  • Start date
M

Michael Ransburg

Hello!

I've got the following XML "template" (for example).

<personalData>
<address>
<name>$var1</name>
<zip>$var2</name>
</address>
<telephone type=$var3>$var4</telephone>
</personalData>


Is there an efficient way to replace the $var* variables by values
using Java? If not, what is an efficient way to work with such
templates?

I'd imagine something like the following:
Template t = new Template("template.xml");
t.var1 = "Michael";
t.var2 = 81825;
t.var3 = "Mobile Phone";
t.var4 = 12345678;
Document d = t.serialize();


Any help would be greatly appreciated!


Best regards
Michael
 
A

Anton Spaans

Michael Ransburg said:
Hello!

I've got the following XML "template" (for example).

<personalData>
<address>
<name>$var1</name>
<zip>$var2</name>
</address>
<telephone type=$var3>$var4</telephone>
</personalData>


Is there an efficient way to replace the $var* variables by values
using Java? If not, what is an efficient way to work with such
templates?

I'd imagine something like the following:
Template t = new Template("template.xml");
t.var1 = "Michael";
t.var2 = 81825;
t.var3 = "Mobile Phone";
t.var4 = 12345678;
Document d = t.serialize();


Any help would be greatly appreciated!


Best regards
Michael

Take a look at XSL transformations. (xml.apache.org and search for the
Xalan2, for example)
-- Anton Spaans.
 
T

Tim Tyler

Michael Ransburg said:
I've got the following XML "template" (for example).

<personalData>
<address>
<name>$var1</name>
<zip>$var2</name>
</address>
<telephone type=$var3>$var4</telephone>
</personalData>

Is there an efficient way to replace the $var* variables by values
using Java? If not, what is an efficient way to work with such
templates?

I'd imagine something like the following:
Template t = new Template("template.xml");
t.var1 = "Michael";
t.var2 = 81825;
t.var3 = "Mobile Phone";
t.var4 = 12345678;
Document d = t.serialize();

Freemarker does pretty-much that:

http://freemarker.sourceforge.net/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top