我有非常简单的速度模板:
<html>
<head>
<title>Velocity template</title>
</head>
<body>
#foreach($p in $products)
$p.name
#end
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
以及处理它的代码:
VelocityEngine engine = new VelocityEngine();
engine.init();
Template t = engine.getTemplate("./src/com/irbis/dms/velocity/template.html");
VelocityContext ctx = new VelocityContext();
Product p1 = new Product("fridge");
Product p2 = new Product("sofa");
Product p3 = new Product("table");
Product p4 = new Product("chair");
List<Product> products = new ArrayList<Product>();
products.add(p1);
products.add(p2);
products.add(p3);
products.add(p4);
ctx.put("products", products);
...
class Product implements Serializable {
private String name;
public Product() {
}
public Product(String name) {
super();
this.name …Run Code Online (Sandbox Code Playgroud) 我有html文件:
<html><head></head><body><div style="font-family: Verdana;font-size: 12.0px;">
<div>Test message.</div>
<div> </div>
<div>More content here...</div>
<div> </div>
<div>Best regards,</div>
<div>Mr. Crowley</div></div></body></html>
Run Code Online (Sandbox Code Playgroud)
我尝试使用Apache Tika获取上面文件的内容......
final InputStream input = new FileInputStream("file.html");
final ContentHandler handler = new BodyContentHandler();
final Metadata metadata = new Metadata();
final HtmlParser htmlParser = new HtmlParser();
htmlParser.parse(input, handler, metadata, new ParseContext());
String plainText = handler.toString();
System.out.println(plainText);
Run Code Online (Sandbox Code Playgroud)
...除了额外的换行符外,一切都很好:
Test message.
More content here...
Best regards,
Mr. Crowley
<and 3 empty lines here>
Run Code Online (Sandbox Code Playgroud)
有可能避免这种行为吗?是否有可能获得更多预期结果:
Test message.
More content here...
Best regards,
Mr. Crowley
Run Code Online (Sandbox Code Playgroud)
?
代码结构如
plainText …Run Code Online (Sandbox Code Playgroud) 是否可以使用java通过使用java API或正则表达式的字符串中的字符来获取单词?
例如我有字符串:
`A broken friendship may be soldered, but will never be sound.`
Run Code Online (Sandbox Code Playgroud)
并希望得到包括所有单词so(焊接和音),并取代它们
<b>soldered</b>和<b>sound</b>
谢谢!