brain_damage
brain_damage

Reputation: 985

Replace templates in html files

Given a dictionary:

1=f00
2=bar
3=larodi
.
.
.

and some html files like:

<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>  
   content content content {1} bla
   bla bla {3} &%$*#$*$%# {2} and so on...
</body>
</html>

How to replace the keys in the files with their values with java? I've read about java.text.MessageFormat, but cannot come up with a way to use it.

Upvotes: 0

Views: 2299

Answers (3)

mhshams
mhshams

Reputation: 16962

String result = MessageFormat.format(htmlString, "foo", "bar", "larodi");


String[] paramas = {"foo" , ....};
String result = MessageFormat.format(htmlString, params);

htmlString is your html content.

Upvotes: 1

extraneon
extraneon

Reputation: 23960

It seems to me that a message formatter does not use dictionaries, but arrays. Your '1' is, as such, not a key but an index.

It looks a bit like you remember the python % operator. This isn't like that. MessageFormat uses a positional system. Either by entering the parameters directly, or by giving an array.

Upvotes: 0

Daniel Moura
Daniel Moura

Reputation: 7966

You may want to check some java template engines like freemarker and velocity.

Upvotes: 1

Related Questions