Created
          April 25, 2021 09:31 
        
      - 
      
 - 
        
Save vyach-vasiliev/1f03790c675d38efb622aad01c073c62 to your computer and use it in GitHub Desktop.  
    Jackson XML to POJO class Test for Kotlin
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | <dependency> | |
| <groupId>com.fasterxml.jackson.core</groupId> | |
| <artifactId>jackson-databind</artifactId> | |
| <version>2.12.3</version> | |
| </dependency> | |
| <dependency> | |
| <groupId>com.fasterxml.jackson.dataformat</groupId> | |
| <artifactId>jackson-dataformat-xml</artifactId> | |
| <version>2.9.6</version> | |
| </dependency> | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import com.fasterxml.jackson.annotation.JsonIgnoreProperties | |
| import com.fasterxml.jackson.dataformat.xml.XmlMapper | |
| import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper | |
| import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty | |
| import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement | |
| import com.fasterxml.jackson.module.kotlin.readValue | |
| import org.junit.jupiter.api.Test | |
| class XML2POJOTest { | |
| @JacksonXmlRootElement(localName = "xyz-objects") | |
| data class XyzObjects( | |
| @JacksonXmlProperty(localName = "version", isAttribute = true) | |
| val version: Any? = "", | |
| @JacksonXmlProperty(localName = "object") | |
| @JacksonXmlElementWrapper(useWrapping = false) | |
| val objects: List<Object>? | |
| ) | |
| @JsonIgnoreProperties(ignoreUnknown = true) | |
| data class Object( | |
| @JacksonXmlProperty(localName = "pk", isAttribute = true) | |
| val pk: String?, | |
| @JacksonXmlProperty(localName = "model", isAttribute = true) | |
| val model: String?, | |
| ) | |
| @Test | |
| fun `XML2POJO`() { | |
| val text = """<xyz-objects version="1.0"> | |
| <object pk="1" model="roll"> | |
| <field type="BigIntegerField" name="roll_number">1000000714</field> | |
| <field type="CharField" name="status">DL</field> | |
| <field name="scans"> | |
| <object pk="1" model="scan_stages"> | |
| <field type="DateTimeField" name="updated_on">11 Jul, 2017, 17:40</field> | |
| </object> | |
| </field> | |
| </object> | |
| </xyz-objects>""" | |
| val root = XmlMapper().readValue<XyzObjects>(text) | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment