Skip to content
项目

关于

Fruit for Html just like Gson for Json.

网站与应用开发

这些数字如何统计

实测星标增长

24小时
0
7天
0
15天
0
1个月
正在收集历史

各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。

README.md

原文

Fruit

Download Build Status

fruit_library.png

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.

探索相关项目

加载中…