同步操作将从 Gitee 极速下载/Fruit-html 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
Fruit is a Java library that can be used to convert html to Java Objects and back follow a certain rule.
So, in short, Fruit for Html just like Gson for Json.
Fruit was inspired by Goolge's Gson, and powered by jsoup.
Let me suppose that you have a html file, just like below:
<html>
<head></head>
<body>
<div id='only'>Apple <em>is my favorite fruit.</em></div>
<img src='http://dwz.cn/5USjpv' class="apple"/>
<a href='https://ghui.me' class="author"> ghui</a>
<div id='fruits'>
<div class="fruit" id="1">
<strong class="name">apple</strong>
<strong class="color">red</strong>
</div>
<div class="fruit" id="2">
<strong class="name">orange</strong>
<strong class="color">green</strong>
</div>
<div class="fruit" id="3">
<strong class="name">banana</strong>
<strong class="color">yellow</strong>
</div>
<div class="fruit" id="4">
<strong class="name">pear</strong>
<strong class="color">yellow</strong>
</div>
<div class="fruit" id="5">
<strong class="name">peach</strong>
<strong class="color">pink</strong>
</div>
</div>
</body>
</html>
And now you want to parse the document to a Java Object named FruitInfo
, like below:
public class FruitInfo {
private String favorite;
private String favoriteOne;
private String favoriteHtml;
private String img;
private String blog;
private List<Item> items;
public static class Item {
private String name;
private String color;
private int id;
}
}
What would you do ? No bullshit, with Fruit you can just do like below:
Pick
annotation to FruitInfo.public class FruitInfo {
@Pick("div#only")
private String favorite;
@Pick(value = "div#only", attr = Attrs.OWN_TEXT)
private String favoriteOne;
@Pick(value = "div#only", attr = Attrs.HTML)
private String favoriteHtml;
@Pick(value = "img.apple", attr = Attrs.SRC)
private String img;
@Pick(value = "a.author", attr = Attrs.HREF)
private String blog;
@Pick("div.fruit")
private List<Item> items;
public static class Item {
@Pick("strong.name")
private String name;
@Pick(".color")
private String color;
@Pick(attr = "id")
private int id;
}
}
FruitInfo fruitInfo = new Fruit().fromHtml(htmlStr, FruitInfo.class);
Note: There are two field of the Pick annotation, the value field is a
css-like element selector
, For more details, visit here.
The second field is optional, and it's default value istext
. You can assign ownText(direct text), src, href, and any other exist attr-name in your html tree to it. Just run the junit test code for more detail.
<dependency>
<groupId>me.ghui</groupId>
<artifactId>Fruit</artifactId>
<version>latest.version</version>
<type>pom</type>
</dependency>
compile 'me.ghui:Fruit:latest.version'
-keeppackagenames org.jsoup.nodes
Copyright 2017 ghui
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。