用于HTML到Java(POJO)转换的Java库
通过使用,我们可以将Java对象(列表、POJO等)与(HTML)组合,并创建(HTML)输出 有没有Java API可以帮助进行反向工程?这个API的输入可以是HTML输出和使用的模板,输出应该是用于生成输出的数据(Java/XML格式)用于HTML到Java(POJO)转换的Java库,java,html,reverse-engineering,templating,Java,Html,Reverse Engineering,Templating,通过使用,我们可以将Java对象(列表、POJO等)与(HTML)组合,并创建(HTML)输出 有没有Java API可以帮助进行反向工程?这个API的输入可以是HTML输出和使用的模板,输出应该是用于生成输出的数据(Java/XML格式) 我知道,但这只是让我提取HTML元素(如表)。我正在寻找基于某个模板提取数据的东西。您可以使用google protobuf来转换不同类型的消息。而且定义模板也很容易。我使用JSON.parse()创建JavaScript对象,在Java中,您可以使用pro
我知道,但这只是让我提取HTML元素(如表)。我正在寻找基于某个模板提取数据的东西。您可以使用google protobuf来转换不同类型的消息。而且定义模板也很容易。我使用JSON.parse()创建JavaScript对象,在Java中,您可以使用protobuf将JSON转换为Java对象
我的答案可能对这个问题的作者没有什么用处(我已经晚了5年,所以我想时机不对),但由于这是我在Google上键入
HTML到POJO
时发现的第一个结果,我认为它可能对许多其他可能遇到这个答案的开发人员有用
今天,我刚刚(以我公司的名义)发布了一个HTML到POJO的完整框架,您可以使用它将HTML映射到任何POJO类,只需一些注释。该库本身非常方便,并具有许多其他功能,同时非常可插拔。您可以在此处查看:
如何使用:基础
假设我们需要解析以下html页面:
<html>
<head>
<title>A Simple HTML Document</title>
</head>
<body>
<div class="restaurant">
<h1>A la bonne Franquette</h1>
<p>French cuisine restaurant for gourmet of fellow french people</p>
<div class="location">
<p>in <span>London</span></p>
</div>
<p>Restaurant n*18,190. Ranked 113 out of 1,550 restaurants</p>
<div class="meals">
<div class="meal">
<p>Veal Cutlet</p>
<p rating-color="green">4.5/5 stars</p>
<p>Chef Mr. Frenchie</p>
</div>
<div class="meal">
<p>Ratatouille</p>
<p rating-color="orange">3.6/5 stars</p>
<p>Chef Mr. Frenchie and Mme. French-Cuisine</p>
</div>
</div>
</div>
</body>
</html>
我们在github页面上提供了关于上述代码的更多解释
现在,让我们看看如何取消这个
private static final String MY_HTML_FILE = "my-html-file.html";
public static void main(String[] args) {
HtmlToPojoEngine htmlToPojoEngine = HtmlToPojoEngine.create();
HtmlAdapter<Restaurant> adapter = htmlToPojoEngine.adapter(Restaurant.class);
// If they were several restaurants in the same page,
// you would need to create a parent POJO containing
// a list of Restaurants as shown with the meals here
Restaurant restaurant = adapter.fromHtml(getHtmlBody());
// That's it, do some magic now!
}
private static String getHtmlBody() throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(MY_HTML_FILE));
return new String(encoded, Charset.forName("UTF-8"));
}
private静态最终字符串MY\u HTML\u FILE=“MY HTML FILE.HTML”;
公共静态void main(字符串[]args){
HtmlToPojoEngine HtmlToPojoEngine=HtmlToPojoEngine.create();
HtmlAdapter=htmlToPojoEngine.adapter(Restaurant.class);
//如果它们是同一页面中的多家餐厅,
//您需要创建一个包含
//如图所示的餐厅列表以及这里的膳食
Restaurant Restaurant=adapter.fromHtml(getHtmlBody());
//就这样,现在施点魔法吧!
}
私有静态字符串getHtmlBody()引发IOException{
byte[]encoded=Files.readAllBytes(path.get(MY_HTML_FILE));
返回新字符串(编码,Charset.forName(“UTF-8”);
}
可以找到另一个简短的例子
希望这将有助于其他人
public class Meal {
@Selector(value = "p:nth-child(1)")
private String name;
@Selector(
value = "p:nth-child(2)",
format = "^([0-9.]+)\/5 stars$",
indexForRegexPattern = 1
)
private Float stars;
@Selector(
value = "p:nth-child(2)",
// rating-color custom attribute can be used as well
attr = "rating-color"
)
private String ratingColor;
@Selector(
value = "p:nth-child(3)"
)
private String chefs;
// getters and setters.
}
private static final String MY_HTML_FILE = "my-html-file.html";
public static void main(String[] args) {
HtmlToPojoEngine htmlToPojoEngine = HtmlToPojoEngine.create();
HtmlAdapter<Restaurant> adapter = htmlToPojoEngine.adapter(Restaurant.class);
// If they were several restaurants in the same page,
// you would need to create a parent POJO containing
// a list of Restaurants as shown with the meals here
Restaurant restaurant = adapter.fromHtml(getHtmlBody());
// That's it, do some magic now!
}
private static String getHtmlBody() throws IOException {
byte[] encoded = Files.readAllBytes(Paths.get(MY_HTML_FILE));
return new String(encoded, Charset.forName("UTF-8"));
}