Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bvolpato/digesteroids
💊 Digester on Steroids for Java
https://github.com/bvolpato/digesteroids
digester html java json mapping pojos xml
Last synced: 2 months ago
JSON representation
💊 Digester on Steroids for Java
- Host: GitHub
- URL: https://github.com/bvolpato/digesteroids
- Owner: bvolpato
- License: apache-2.0
- Created: 2016-08-26T20:51:57.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-11-20T20:40:57.000Z (about 7 years ago)
- Last Synced: 2024-04-14T02:55:03.100Z (8 months ago)
- Topics: digester, html, java, json, mapping, pojos, xml
- Language: Java
- Homepage:
- Size: 54.7 KB
- Stars: 9
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
digesteroids
========[![Apache License](http://img.shields.io/badge/license-ASL-blue.svg)](https://github.com/brunocvcunha/digesteroids/blob/master/LICENSE)
[![Build Status](https://travis-ci.org/brunocvcunha/digesteroids.svg)](https://travis-ci.org/brunocvcunha/digesteroids)
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.brunocvcunha.digesteroids/digesteroids/badge.svg)](https://maven-badges.herokuapp.com/maven-central/digesteroids/digesteroids)
[![Coverage Status](https://coveralls.io/repos/github/brunocvcunha/digesteroids/badge.svg?branch=master)](https://coveralls.io/github/brunocvcunha/digesteroids?branch=master):pill: Digester on Steroids.
Map data to your POJOs in a beautiful way. Supports mapping from multiple sources:
1. HTML (scraping), using XPath, ID or CSS selector.
2. JSON Path
3. Maps
4. Property filesDownload
--------Download [the latest JAR][1] or grab via Maven:
```xmlorg.brunocvcunha.digesteroids
digesteroids
0.3```
or Gradle:
```groovy
compile 'org.brunocvcunha.digesteroids:digesteroids:0.3'
```Snapshots of the development version are available in [Sonatype's `snapshots` repository][snap].
Digesteroids requires at minimum Java 8.
Examples
--------### Convert a HTML into a POJO, based on the IDs, CSS Selector or XPath
example.html
```html
Digesteroids Test
Bruno Candido Volpato da Cunha
24
65535 University Ave
Palo Alto
```
PersonPOJO.java
```java
@DigesterEntity
public class PersonPOJO {
@DigesterMapping(source = "htmlExample", refType = ReferenceTypeEnum.HTML_ID, value = "fname")
private String name;
@DigesterMapping(source = "htmlExample", refType = ReferenceTypeEnum.HTML_ID, value = "age")
private Integer age;
@DigesterMapping(source = "htmlExample", refType = ReferenceTypeEnum.PASS_THROUGH, value = "")
private AddressPOJO address;
}@DigesterEntity
public class AddressPOJO {
@DigesterMapping(source = "htmlExample", refType = ReferenceTypeEnum.HTML_CSS, value = "p.address1")
private String address1;
@DigesterMapping(source = "htmlExample", refType = ReferenceTypeEnum.HTML_XPATH, value = "p > span")
private String city;
}
```Usage
```java
Digesteroids digister = new Digesteroids();PersonPOJO person = digister.convertHTMLToType("htmlExample", getClass().getResourceAsStream("/example.html"), PersonPOJO.class);
assertEquals("Bruno Candido Volpato da Cunha", person.getName());
assertEquals("65535 University Ave", person.getAddress().getAddress1());
assertEquals("Palo Alto", person.getAddress().getCity());
```### Convert a Map or Json into a POJO, converting the names:
POJOs:
```java
@DigesterEntity
public class PersonPOJO {
// Field can have multiple @DigesterMapping
@DigesterMapping(source = "htmlExample", refType = ReferenceTypeEnum.HTML_ID, value = "fname")
@DigesterMapping(value = "fullName")
private String name;
@DigesterMapping(source = "htmlExample", refType = ReferenceTypeEnum.HTML_ID, value = "age")
@DigesterMapping(value = "age")
private Integer age;
@DigesterMapping(value = "personAddress")
private AddressPOJO address;
}@DigesterEntity
public class AddressPOJO {
@DigesterMapping(value = "address")
private String address1;
@DigesterMapping(value = "addressCity")
private String city;
}
```Usage (Map to POJO):
```java
Digesteroids digister = new Digesteroids();Map personMap = new LinkedHashMap<>();
personMap.put("fullName", "Bruno");
personMap.put("age", "24");Map addressMap = new LinkedHashMap<>();
addressMap.put("address", "65535 University Ave");
addressMap.put("addressCity", "Palo Alto");personMap.put("personAddress", addressMap);
PersonPOJO person = digister.convertObjectToType(personMap, PersonPOJO.class);
assertEquals("Bruno", person.getName());
assertEquals("65535 University Ave", person.getAddress().getAddress1());
assertEquals("Palo Alto", person.getAddress().getCity());
```[1]: https://search.maven.org/remote_content?g=org.brunocvcunha.digesteroids&a=digesteroids&v=LATEST
[snap]: https://oss.sonatype.org/content/repositories/snapshots/