1 Star 0 Fork 1

Glony / Fruit-html

forked from Gitee 极速下载 / Fruit-html 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

Fruit

Download Build Status

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.

Example

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:

  1. Add 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;
    }
}
  1. Just do pick from the html.
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 is text. 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.

Who uses Fruit ?

  1. V2er - A Nice V2EX App.

Download

  • Maven
<dependency>
  <groupId>me.ghui</groupId>
  <artifactId>Fruit</artifactId>
  <version>latest.version</version>
  <type>pom</type>
</dependency>
  • Gradle
compile 'me.ghui:Fruit:latest.version'

Proguad

-keeppackagenames org.jsoup.nodes

Thanks

  1. Gson
  2. jsoup

License

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.

空文件

简介

Fruit 简单的说Fruit之于html正如Gson之于Json,它能让你以一种注解的方式来描述解析的逻辑 展开 收起
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/glony/Fruit-html.git
git@gitee.com:glony/Fruit-html.git
glony
Fruit-html
Fruit-html
master

搜索帮助