xml
xml is the realization of java xml framework.
Hope in the most elegant way to carry out the conversion process between the xml and java, one line of code to get everything.
Feature
-
Xml mapping of objects and each other
-
Support notes
@Alias
alias - Support annotation
@Ignore
to specify ignored field
Change Log
Quick Start
ready
jdk 1.7+
maven 3.x+
maven introduced
<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>xml-mapping</artifactId>
<version>0.0.3</version>
</dependency>
Converted to xml
User user = defaultUser();
String xml = XmlMappingBs.newInstance().toXml(user);
See User class User.java
Converted to a java object
XML is xml string corresponding to the content.
User user = XmlMappingBs.newInstance().toBean(XML, User.class);
Alias
Explanatory notes
@Alias
Can be placed on the specified class and field, we want to specify another name for information.
Use Cases
- Object Definition
@Alias("user")
public class AliasUser {
@Alias("nickname")
private String name;
private String hobby;
}
- Test code
AliasUser user = defaultAliasUser();
String xml = XmlMappingBs.newInstance().toXml(user);
For details, see XmlMappingBsAliasTest.java
- Generating effect
<?xml version="1.0" encoding="UTF-8"?>
<user>
<nickname>hello</nickname>
<hobby>world</hobby>
</user>
Ignore the specified field
Explanation
Sometimes we want some fields are not involved in xml conversion, you can @Ignore
specify neglected field.
Use Cases
- Object Definition
public class IgnoreUser {
private String name;
@Ignore
private String hobby;
}
- Test code
And original use exactly the same details, see XmlMappingBsIgnoreTest.java
IgnoreUser user = defaultIgnoreUser();
String xml = XmlMappingBs.newInstance().toXml(user);
result
<?xml version="1.0" encoding="UTF-8"?>
<IgnoreUser>
<name>hello</name>
</IgnoreUser>