css 解析器 java_jspoon是一个Java库,它基于CSS选择器提供将Java解析为Java对象

68747470733a2f2f6d6176656e2d6261646765732e6865726f6b756170702e636f6d2f6d6176656e2d63656e7472616c2f706c2e64726f6964736f6e726f6964732f6a73706f6f6e2f62616467652e7376673f7374796c653d666c617468747470733a2f2f6a617661646f632e696f2f62616467652f706c2e64726f6964736f6e726f6964732f6a73706f6f6e2e7376673f636f6c6f723d626c7565

jspoon

jspoon is a Java library that provides parsing HTML into Java objects basing on CSS selectors. It uses jsoup underneath as a HTML parser.

Installation

Insert the following dependency into your project's build.gradle file:

dependencies {

implementation 'pl.droidsonroids:jspoon:1.3.2'

}

Usage

jspoon works on any class with a default constructor. To make it work you need to annotate fields with @Selector annotation and set a CSS selector as the annotation's value:

class Page {

@Selector("#title") String title;

@Selector("li.a") List intList;

@Selector(value = "#image1", attr = "src") String imageSource;

}

Then you can create a HtmlAdapter and use it to build objects:

String htmlContent = "

"

+ "

Title

"

+ "

  • "

+ "

1"

+ "

2"

+ "

3"

+ "

"

+ "image.bmp"

+ "

";

Jspoon jspoon = Jspoon.create();

HtmlAdapter htmlAdapter = jspoon.adapter(Page.class);

Page page = htmlAdapter.fromHtml(htmlContent);

//title = "Title"; intList = [1, 3]; imageSource = "image.bmp"

It looks for the first occurrence in HTML and sets its value to a field.

Supported types

@Selector can be applied to any field of the following types (or their primitive equivalents):

String

Boolean

Integer

Long

Float

Double

Date

BigDecimal

Jsoup's Element

Any class with default constructor

List (or its superclass/superinterface) of supported type

It can also be used with a class, then you don't need to annotate every field inside it.

Attributes

By default, the HTML's textContent value is used on Strings, Dates and numbers. It is possible to use an attribute by setting an attr parameter in the @Selector annotation. You can also use "html" (or "innerHtml") and "outerHtml" as attr's value.

Formatting and regex

Regex can be set up by passing regex parameter to @Selector annotation. Example:

class Page {

@Selector(value = "#numbers", regex = "([a-z]+),") String matchedNumber;

}

Date format can be set up by passing value parameter to @Format annotation. Example:

class Page {

@Format(value = "HH:mm:ss dd.MM.yyyy")

@Selector(value = "#date") Date date;

}

String htmlContent = "13:30:12 14.07.2017"

+ "ONE, TwO, three,";

Jspoon jspoon = Jspoon.create();

HtmlAdapter htmlAdapter = jspoon.adapter(Page.class);

Page page = htmlAdapter.fromHtml(htmlContent);//date = Jul 14, 2017 13:30:12; matchedNumber = "three";

Java's Locale is used for parsing Floats, Doubles and Dates. You can override it by setting languageTag @Format parameter:

@Format(languageTag = "pl")

@Selector(value = "div > p > span") Double pi; //3,14 will be parsed

If jspoon doesn't find a HTML element it wont't set field's value unless you set the defValue parameter:

@Selector(value = "div > p > span", defValue = "NO_TEXT") String text;

Custom converterts

When format or regex is not enough, custom converter can be used to implement parsing from jsoup's Element. This can be done by extending ElementConverter class:

public class JoinChildrenClassConverter implements ElementConverter {

@Override

public String convert(Element node, Selector selector) {

return node.children().stream().map(Element::text).collect(Collectors.joining(", "));

}

}

And it can be used the following way:

public class Model {

@Selector(value = "#id", converter = JoinChildrenClassConverter::class)

String childrenText;

}

Retrofit

Retrofit converter is available here.

Changelog

Other libraries/inspirations

jsoup - all HTML parsing in jspoon is made by this library

webGrude - when I had an idea I found this library. It was the biggest inspiration and I used some ideas from it

Moshi - I wanted to make jspoon work with HTML the same way as Moshi works with JSON. I adapted caching mechanism (fields and adapters) from it.

jsoup-annotations - similar to jspoon

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值