XML "templates" and Java (?)

Michael Ransburg

Usenet Archive
Joined
Feb 9, 2004
Messages
10
Reaction score
0
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
 

Arto Viitanen

Usenet Archive
Joined
Dec 1, 2000
Messages
62
Reaction score
0
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.
 

Charles Fineman

Usenet Archive
Joined
Dec 18, 2003
Messages
13
Reaction score
0
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

No members online now.

Forum statistics

Threads
548,208
Messages
3,104,799
Members
249,594
Latest member
henrywalsh

Latest Threads

Top