https://github.com/pityubak/xmlgrinder
https://github.com/pityubak/xmlgrinder
annotations xml xml-parsing xml-serialization
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/pityubak/xmlgrinder
- Owner: Pityubak
- Created: 2020-02-23T18:24:57.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2020-06-05T19:37:22.000Z (almost 6 years ago)
- Last Synced: 2025-04-01T15:48:45.538Z (about 1 year ago)
- Topics: annotations, xml, xml-parsing, xml-serialization
- Language: Java
- Size: 28.3 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# XmlGrinder
## Overview
#### Serializing a simple object:
```java
@Xml
public class Single {
@Attribute
private int id;
@XmlElement
private String name;
@XmlElement
private int age;
public Single(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
//getters and setters here(optional)
}
```
To serialize an instance of the above object a XmlGrinder is required.
```java
XmlGrinder grinder=new XmlGrinder();
Single single=new Single(2,"Single",20);
grinder.write(single, "E:\\single.xml");
```
And output:
```xml
Single
20
```
#### Deserializing a simple object:
Deserialization is really easy, the read method is used, which produces an instance of the annotated object.
```java
XmlGrinder grinder = new XmlGrinder();
Single single=grinder.read(Single.class, "E:\\single.xml");
```
#### Serializing nested class and list
To serialize more complex object the empty constructor always is required.
```java
@Xml
public class Nested {
@Attribute
private String name;
@XmlElement
private String address;
@XmlElement
private String email;
//Empty constructor is neccesary
public Nested() {
}
public Nested(String name, String address, String email) {
this.name = name;
this.address = address;
this.email = email;
}
//getters and setters here(optional)
}
@Xml
public class NestedWithList {
@Attribute
private int index;
@XmlElement
private String name;
@XmlElement
private Nested nested;
@XmlList
private List singles;
//Empty constructor is necessary
public NestedWithList() {
}
public NestedWithList(int index, String name, Nested nested, List singles) {
this.index = index;
this.name = name;
this.nested = nested;
this.singles = singles;
}
//getters and setters here(optional)
}
```
And the output:
```xml
nestedWithList
Gotham
nested@withlist.com
Joker
34
Batman
66
```
#### Deserializing a nested object and/or list:
Deserialization of complex object is similar as above.
```java
XmlGrinder grinder = new XmlGrinder();
NestedWithList nestedWithList=grinder.read(NestedWithList.class, "E:\\nest.xml");
```
#### Limitations
It does not support namespace or any other type of collection. More efficient version is possible, some algorithm will be better
and it would be worth to write custom parser/and writer.
#### Note
This is example project of Liberator 0.3