spring-xom-unmarshaller

Spring XML Unmarshalling with XOM

License

License

GroupId

GroupId

com.itelg.spring
ArtifactId

ArtifactId

spring-xom-unmarshaller
Last Version

Last Version

1.4.0
Release Date

Release Date

Type

Type

jar
Description

Description

spring-xom-unmarshaller
Spring XML Unmarshalling with XOM
Project URL

Project URL

https://github.com/julian-eggers/spring-xom-unmarshaller
Source Code Management

Source Code Management

https://github.com/julian-eggers/spring-xom-unmarshaller

Download spring-xom-unmarshaller

How to add to project

<!-- https://jarcasting.com/artifacts/com.itelg.spring/spring-xom-unmarshaller/ -->
<dependency>
    <groupId>com.itelg.spring</groupId>
    <artifactId>spring-xom-unmarshaller</artifactId>
    <version>1.4.0</version>
</dependency>
// https://jarcasting.com/artifacts/com.itelg.spring/spring-xom-unmarshaller/
implementation 'com.itelg.spring:spring-xom-unmarshaller:1.4.0'
// https://jarcasting.com/artifacts/com.itelg.spring/spring-xom-unmarshaller/
implementation ("com.itelg.spring:spring-xom-unmarshaller:1.4.0")
'com.itelg.spring:spring-xom-unmarshaller:jar:1.4.0'
<dependency org="com.itelg.spring" name="spring-xom-unmarshaller" rev="1.4.0">
  <artifact name="spring-xom-unmarshaller" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.itelg.spring', module='spring-xom-unmarshaller', version='1.4.0')
)
libraryDependencies += "com.itelg.spring" % "spring-xom-unmarshaller" % "1.4.0"
[com.itelg.spring/spring-xom-unmarshaller "1.4.0"]

Dependencies

compile (3)

Group / Artifact Type Version
org.springframework : spring-oxm jar 5.2.8.RELEASE
com.itelg : xpath-helper jar 0.6.0
commons-io : commons-io jar 2.7

provided (2)

Group / Artifact Type Version
org.springframework.boot : spring-boot-autoconfigure jar 2.3.2.RELEASE
org.springframework.amqp : spring-amqp jar 2.2.9.RELEASE

test (7)

Group / Artifact Type Version
org.assertj : assertj-core jar 3.16.1
org.easymock : easymock jar 4.2
org.powermock : powermock-module-junit4 jar 2.0.7
org.powermock : powermock-api-easymock jar 2.0.7
org.jacoco : org.jacoco.agent jar 0.8.5
org.junit.jupiter : junit-jupiter-engine jar 5.6.2
org.junit.vintage : junit-vintage-engine jar 5.6.2

Project Modules

There are no modules declared in this project.

spring-xom-unmarshaller

Maven Central Build Nightly build

Spring XML Unmarshalling with XOM

Maven

<dependency>
  <groupId>com.itelg.spring</groupId>
  <artifactId>spring-xom-unmarshaller</artifactId>
  <version>1.4.0</version>
</dependency>

Configuration

Enable auto-configuration via annotation

@Autowire XomUnmarshaller for further use in MarshallingHttpMessageConverter or MarshallingMessageConverter.

@SpringBootApplication
@EnableXomUnmarshaller
public class Application
{
    @Autowired
    private XomUnmarshaller xomUnmarshaller;
    
    public static void main(String[] args) throws Exception
    {
        SpringApplication.run(Application.class, args);
    }
}

Parser resolving

A matching parser can be resolved either via return-type, root-tag or an xpath-expression.

Root-tag resolving via return-type (Root-Tag: integer)
@Component
public class IntegerParser implements Parser<Integer>
{
    @Override
    public Integer parse(Element rootElement)
    {
        return XPathHelper.getInteger("data/@value", rootElement);
    }
}
Root-tag resolving via annotation (Root-Tag: text, blob or string)
@Component
@RootTagMatcher("text")
@RootTagMatcher("blob")
public class TextParser implements Parser<String>
{
    @Override
    public Integer parse(Element rootElement)
    {
        return XPathHelper.getString("data/@value", rootElement);
    }
}
Disable type-resolving via annotation (Root-Tag: text)
@Component
@RootTagMatcher("text")
@DisableRootTagTypeMatcher
public class TextParser implements Parser<String>
{
    @Override
    public Integer parse(Element rootElement)
    {
        return XPathHelper.getString("data/@value", rootElement);
    }
}
Resolving via xpath-expression
@Component
@XPathExpressionMatcher("//response/customer")
public class CustomerParser implements Parser<Customer>
{
    @Override
    public Customer parse(Element rootElement)
    {
        Customer customer = new Customer();
        customer.setId(XPathHelper.getPLong("//response/customer/id", rootElement));
        return customer;
    }
}
Resolving via xpath-expression-value
@Component
@XPathExpressionMatcher(value = "//response/@type", expressionValue = "customer")
public class XPathExpressionValueCustomerParser implements Parser<Customer>
{
    @Override
    public Customer parse(Element rootElement)
    {
        Customer customer = new Customer();
        customer.setId(XPathHelper.getPLong("//response/data/id", rootElement));
        return customer;
    }
}

Additional features

Pre-parse interceptor

If you have to work with invalid XML`s you can create an interceptor to remove invalid chars or to fix the structure.

@Bean
public XomUnmarshaller xomUnmarshaller(List<Parser<?>> parsers)
{
    return new XomUnmarshaller(parsers, xmlChars ->
    {
        var xml = new String(xmlChars);
        xml = xml.replace("&#x1e;", "");
        xml = xml.replace("&#x1f;", "");
        xml = xml.replace("&#x0;", "");
        xml = xml.replace("&#xb;", "");

        return xml.getBytes();
    });
}

Test-Support

Parser<?> parser = new IntegerParser();
Assert.assertTrue(XomUnmarshallerTestUtil.resolves(parser, "<integer><data value=\"11\" /></integer>"));

Build & Release

Build

mvn clean package

Release

mvn clean deploy

Versions

Version
1.4.0
1.4.0-RC4
1.4.0-RC2
1.4.0-RC1
1.3.0
1.2.0-RELEASE
1.1.0-RELEASE
1.0.4-RELEASE
1.0.3-RELEASE
1.0.2-RELEASE
1.0.1-RELEASE
1.0.0-RELEASE
1.0.0-RC1
0.2.2-RELEASE
0.2.1-RELEASE
0.2.0-RELEASE
0.1.1-RELEASE
0.1.0-RELEASE
0.0.2-RELEASE
0.0.1-RELEASE