XML "templates" and Java (?)

  • 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

Arto Viitanen

Michael> Hello! I've got the following XML "template" (for example).

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


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

You might like to try entities. So, instead of $var, you have &var;, which
you program could fill.
 
C

Charles Fineman

That's a nice elegant solution Arto... the first one that came to my
mind was to use Velocity (find it at apache.org). It's a decent generic
template language/engine. You can put any bean into a template's context
and do exactly what you are describing here.

If it's something you need to do "on-line" (e.g. to help generate
responses from a server) then you might want to consider using Velocity.
I would say Arto's idea (while I like it) might be more of
pain-in-the-butt to do on-line (though I suppose you could write a sax
entity resolver do to the replacements for you (though I've never used
the entity resolver interface so I'm no positive that's what it's for).

I do think, however, Velocity might be a better choice if only because
it provides more "out of the box" given that you want to do this from Java.
 

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

Forum statistics

Threads
473,995
Messages
2,570,233
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top