开发者

Replace templates in html files

开发者 https://www.devze.com 2023-01-12 10:43 出处:网络
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\">

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} b开发者_运维知识库la
   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.


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


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

htmlString is your html content.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消