473,385 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

XML "templates" and Java (?)

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
Jul 20 '05 #1
2 3600
>>>>> "Michael" == Michael Ransburg <mi**@unfolded.com> writes:

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.

--
Arto V. Viitanen av@cs.uta.fi
University of Tampere, Department of Computer Sciences
Tampere, Finland http://www.cs.uta.fi/~av/
Jul 20 '05 #2
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.

Arto Viitanen wrote:
>>"Michael" == Michael Ransburg <mi**@unfolded.com> writes:

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.


Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Stefan Siegl | last post by:
Hello, I am trying to learn XSLT to use it in another project. I start reading the book "Java and XSLT" and tried the examples and they are went quite fine (how suprising *g*). Then I tried...
2
by: Markus Dehmann | last post by:
I'd like to process text or document templates that use "embedded C++". Here is a constructed example (the texts I have in mind are much longer and contain relatively few code blocks): ...
205
by: Jeremy Siek | last post by:
CALL FOR PAPERS/PARTICIPATION C++, Boost, and the Future of C++ Libraries Workshop at OOPSLA October 24-28, 2004 Vancouver, British Columbia, Canada http://tinyurl.com/4n5pf Submissions
3
by: Generic Usenet Account | last post by:
This is a two-part question. (1) I have implemented a "Datastructure Registry" template class. I am getting no compiler warnings with older compilers, but newer compilers are generating the...
9
by: Gomaw Beoyr | last post by:
Two question about the "partial classes" (in the next wersion of ..NET). Question 1 ========== Will partial classes (in the next version of C#) have to be declared "partial" in ALL places. ...
4
by: sods | last post by:
Hi, I write a test code about template used for strategy. it's very similar to sample code in TC++PL 13.4.1. #include <iostream> #include <string> using std::basic_string;
4
by: Sacha | last post by:
I'm aware, that up to date, "typedef templates" are not defined within the C++ standard. The seemingly common workaround is this: template <class T> struct MyTypeDef { /* ultimately I need...
169
by: JohnQ | last post by:
(The "C++ Grammer" thread in comp.lang.c++.moderated prompted this post). It would be more than a little bit nice if C++ was much "cleaner" (less complex) so that it wasn't a major world wide...
0
by: Steven Prasil | last post by:
When I start my VisualStudio 2005 it creates automatically new folders: D:\work\Visual Studio 2005 D:\work\Visual Studio 2005\Projects D:\work\Visual Studio 2005\Templates Yes, in VS menu ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.