diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81a66a0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/target/ +/.settings/ +/.classpath +/.project +/build/ +/.idea/ +*.iml + diff --git a/README.md b/README.md new file mode 100644 index 0000000..5fe1a04 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +## XML Utils ## + +A collection of useful XML utilities + +Object to XML + +```java +String xml = XmlUtils.objectToXml(myObject); +``` + +XML to Object + +```java +MyClass myObject = XmlUtils.xmlToObject(xml, MyClass.class); +``` diff --git a/lombok.config b/lombok.config new file mode 100644 index 0000000..a23edb4 --- /dev/null +++ b/lombok.config @@ -0,0 +1,2 @@ +config.stopBubbling = true +lombok.addLombokGeneratedAnnotation = true \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..0eaccc4 --- /dev/null +++ b/pom.xml @@ -0,0 +1,39 @@ + + 4.0.0 + + com.ljunggren + xml-utils + 0.0.1-SNAPSHOT + jar + + xml-utils + http://maven.apache.org + + + UTF-8 + 1.8 + 1.8 + + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + 2.13.1 + + + org.projectlombok + lombok + 1.18.12 + provided + + + junit + junit + 4.13 + test + + + diff --git a/src/main/java/com/ljunggren/xmlUtils/XmlUtils.java b/src/main/java/com/ljunggren/xmlUtils/XmlUtils.java new file mode 100644 index 0000000..d0933a0 --- /dev/null +++ b/src/main/java/com/ljunggren/xmlUtils/XmlUtils.java @@ -0,0 +1,17 @@ +package com.ljunggren.xmlUtils; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.dataformat.xml.XmlMapper; + +public class XmlUtils { + + public static String objectToXml(Object object) throws JsonProcessingException { + return new XmlMapper().writeValueAsString(object); + } + + public static T xmlToObject(String xml, Class clazz) throws JsonMappingException, JsonProcessingException { + return new XmlMapper().readValue(xml, clazz); + } + +} diff --git a/src/test/java/com/ljunggren/xmlUtils/XmlUtilsTest.java b/src/test/java/com/ljunggren/xmlUtils/XmlUtilsTest.java new file mode 100644 index 0000000..53c725e --- /dev/null +++ b/src/test/java/com/ljunggren/xmlUtils/XmlUtilsTest.java @@ -0,0 +1,43 @@ +package com.ljunggren.xmlUtils; + +import static org.junit.Assert.*; + +import org.junit.Test; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +public class XmlUtilsTest { + + @Data + @NoArgsConstructor + @AllArgsConstructor + private static class User { + private String name; + private int age; + @JacksonXmlProperty(isAttribute = true) + private boolean active; + } + + @Test + public void objectToXmlTest() throws JsonProcessingException { + User user = new User("Alex", 40, true); + String expected = "Alex40"; + String actual = XmlUtils.objectToXml(user); + assertEquals(expected, actual); + } + + @Test + public void xmlToObjectTest() throws JsonMappingException, JsonProcessingException { + User user = new User("Alex", 40, true); + String xml = XmlUtils.objectToXml(user); + User generatedUser = XmlUtils.xmlToObject(xml, User.class); + assertEquals(user, generatedUser); + } + +}