xml

XML is an XML framework implemented by Java.

You want to do the conversion between XML and Java in the most elegant way possible, with a single line of code.

The characteristics of

  • Object to XML

  • Support annotation @alias to specify an Alias

  • The @ignore annotation is supported to specify ignored fields

Change log

CHANGE_LOG

Quick start

To prepare

JDK 1.7 +

maven 3.x+

Maven is introduced into

<dependency>
    <groupId>com.github.houbb</groupId>
    <artifactId>xml-mapping</artifactId>
    <version>0.0.3</version>
</dependency>
Copy the code

Converted to XML

User user = defaultUser();
String xml = XmlMappingBs.newInstance().toXml(user);
Copy the code

User See class user.java

To a Java object

XML is the corresponding XML string content.

User user = XmlMappingBs.newInstance().toBean(XML, User.class);
Copy the code

Specify an alias

Note that

@alias can be placed on specified classes and fields to specify the Alias information we want.

Use case

  • Object definitions
@Alias("user")
public class AliasUser {

    @Alias("nickname")
    private String name;

    private String hobby;
}
Copy the code
  • The test code
AliasUser user = defaultAliasUser();
String xml = XmlMappingBs.newInstance().toXml(user);
Copy the code

Details see XmlMappingBsAliasTest. Java

  • Generation effect
<?xml version="1.0" encoding="UTF-8"? >

<user>
    <nickname>hello</nickname>
    <hobby>world</hobby>
</user>
Copy the code

Ignore specified fields

instructions

Sometimes you want certain fields not to participate in the XML transformation, so you can specify which fields to Ignore via @ignore.

Use case

  • Object definitions
public class IgnoreUser {

    private String name;

    @Ignore
    private String hobby;

}
Copy the code
  • The test code

And the original use exactly the same, for details see XmlMappingBsIgnoreTest. Java

IgnoreUser user = defaultIgnoreUser();
String xml = XmlMappingBs.newInstance().toXml(user);
Copy the code

The results of

<?xml version="1.0" encoding="UTF-8"? >

<IgnoreUser>
    <name>hello</name>
</IgnoreUser>
Copy the code

Configuration framework

json

property

csv

xml