Thursday, May 3, 2018

XML parsing with jackson fasterxml

The code is taken from the excellent http://www.baeldung.com/jackson-xml-serialization-and-deserialization


All their source code is available in https://github.com/eugenp/tutorials/tree/master/jackson


package org.pierre.xmlparsing;

public class SimpleBean {
    private int x = 1;
    private int y = 2;
 public int getX() {
  return x;
 }
 public void setX(int x) {
  this.x = x;
 }
 public int getY() {
  return y;
 }
 public void setY(int y) {
  this.y = y;
 }
    
}

package org.pierre.xmlparsing;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.junit.jupiter.api.Test;
import org.pierre.xmlparsing.SimpleBean;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

public class XMLParsingTest {
 /**
  * convert Java bean into XML
  * @throws JsonProcessingException
  */
 @Test
 public void whenJavaSerializedToXmlStr_thenCorrect() throws JsonProcessingException {
     XmlMapper xmlMapper = new XmlMapper();
     String xml = xmlMapper.writeValueAsString(new SimpleBean());
     assertNotNull(xml);
     System.out.println(xml);
 }

 /**
  * convert Java bean into xml and write to file
  * @throws IOException
  */
 @Test
 public void whenJavaSerializedToXmlFile_thenCorrect() throws IOException {
     XmlMapper xmlMapper = new XmlMapper();
     xmlMapper.writeValue(new File("simple_bean.xml"), new SimpleBean());
     File file = new File("simple_bean.xml");
     assertNotNull(file);
 }
 
 
 /**
  * Convert Xml into Java bean 
  * @throws IOException
  */
 @Test
 public void whenJavaGotFromXmlStr_thenCorrect() throws IOException {
     XmlMapper xmlMapper = new XmlMapper();
     SimpleBean value = 
       xmlMapper.readValue("12", 
       SimpleBean.class);
     assertTrue(value.getX() == 1 && value.getY() == 2);
 }
 
 /**
  * Read XML file and convert to Java bean
  * @throws IOException
  */
 @Test
 public void whenJavaGotFromXmlFile_thenCorrect() throws IOException {
     File file = new File("simple_bean.xml");
     XmlMapper xmlMapper = new XmlMapper();
     String xml = inputStreamToString(new FileInputStream(file));
     SimpleBean value = xmlMapper.readValue(xml, SimpleBean.class);
     assertTrue(value.getX() == 1 && value.getY() == 2);
 }
 
 public static String inputStreamToString(InputStream is) throws IOException {
     StringBuilder sb = new StringBuilder();
     String line;
     BufferedReader br = new BufferedReader(new InputStreamReader(is));
     while ((line = br.readLine()) != null) {
         sb.append(line);
     }
     br.close();
     return sb.toString();
 }
}


the pom.xml is:

<project xmlns="http://maven.apache.org/POM/4.0.0"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>org.pierre</groupId>
 <artifactId>pvnexus</artifactId>
 <version>1.0</version>

 <build>
  <sourceDirectory>src/main/java</sourceDirectory>
  <plugins>
   <plugin>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.7.0</version>
    <configuration>
     <source>1.8</source>
     <target>1.8</target>
    </configuration>
   </plugin>
  </plugins>
 </build>
 <dependencies>
  <dependency>
   <groupId>com.fasterxml.jackson.dataformat</groupId>
   <artifactId>jackson-dataformat-xml</artifactId>
   <version>2.9.5</version>
  </dependency>
 </dependencies>
</project>


For more advanced cases you can annotate the Bean with @JsonProperty, but this is not required for basic parsing.



No comments: