Code Monkey home page Code Monkey logo

fruit's Introduction

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.

fruit's People

Contributors

graycreate avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.