Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/julian-eggers/spring-xom-unmarshaller
Spring XML Unmarshalling with XOM
https://github.com/julian-eggers/spring-xom-unmarshaller
java spring spring-boot xml xom xpath
Last synced: about 1 month ago
JSON representation
Spring XML Unmarshalling with XOM
- Host: GitHub
- URL: https://github.com/julian-eggers/spring-xom-unmarshaller
- Owner: julian-eggers
- License: mit
- Created: 2016-07-03T14:50:49.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2021-07-23T04:16:31.000Z (over 3 years ago)
- Last Synced: 2024-10-27T20:58:20.016Z (3 months ago)
- Topics: java, spring, spring-boot, xml, xom, xpath
- Language: Java
- Size: 90.8 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# spring-xom-unmarshaller
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.itelg.spring/spring-xom-unmarshaller/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.itelg.spring/spring-xom-unmarshaller)
[![Build](https://github.com/julian-eggers/spring-xom-unmarshaller/workflows/release/badge.svg)](https://github.com/julian-eggers/spring-xom-unmarshaller/actions)
[![Nightly build](https://github.com/julian-eggers/spring-xom-unmarshaller/workflows/nightly/badge.svg)](https://github.com/julian-eggers/spring-xom-unmarshaller/actions)Spring XML Unmarshalling with [XOM](http://www.xom.nu/)
#### Maven
```xmlcom.itelg.spring
spring-xom-unmarshaller
1.4.0```
#### Configuration
##### Enable auto-configuration via annotation
[@Autowire](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/beans/factory/annotation/Autowire.html) XomUnmarshaller for further use in [MarshallingHttpMessageConverter](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/converter/xml/MarshallingHttpMessageConverter.html) or [MarshallingMessageConverter](http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/messaging/converter/MarshallingMessageConverter.html).
```java
@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)
```java
@Component
public class IntegerParser implements Parser
{
@Override
public Integer parse(Element rootElement)
{
return XPathHelper.getInteger("data/@value", rootElement);
}
}
```##### Root-tag resolving via annotation (Root-Tag: text, blob or string)
```java
@Component
@RootTagMatcher("text")
@RootTagMatcher("blob")
public class TextParser implements Parser
{
@Override
public Integer parse(Element rootElement)
{
return XPathHelper.getString("data/@value", rootElement);
}
}
```##### Disable type-resolving via annotation (Root-Tag: text)
```java
@Component
@RootTagMatcher("text")
@DisableRootTagTypeMatcher
public class TextParser implements Parser
{
@Override
public Integer parse(Element rootElement)
{
return XPathHelper.getString("data/@value", rootElement);
}
}
```##### Resolving via xpath-expression
```java
@Component
@XPathExpressionMatcher("//response/customer")
public class CustomerParser implements Parser
{
@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
```java
@Component
@XPathExpressionMatcher(value = "//response/@type", expressionValue = "customer")
public class XPathExpressionValueCustomerParser implements Parser
{
@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.```java
@Bean
public XomUnmarshaller xomUnmarshaller(List> parsers)
{
return new XomUnmarshaller(parsers, xmlChars ->
{
var xml = new String(xmlChars);
xml = xml.replace("", "");
xml = xml.replace("", "");
xml = xml.replace("", "");
xml = xml.replace("", "");return xml.getBytes();
});
}
```#### Test-Support
```java
Parser> parser = new IntegerParser();
Assert.assertTrue(XomUnmarshallerTestUtil.resolves(parser, ""));
```## Build & Release
### Build
```
mvn clean package
```### Release
```
mvn clean deploy
```