Skip to content

Instantly share code, notes, and snippets.

@maguzzi
Created July 7, 2024 09:08
Show Gist options
  • Save maguzzi/f38b6f94ade32c9fe5a8f21db55670f1 to your computer and use it in GitHub Desktop.
Save maguzzi/f38b6f94ade32c9fe5a8f21db55670f1 to your computer and use it in GitHub Desktop.
minimal setup for generating pojo from xsd - java 21
import org.junit.Test;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.w3schools.Note;
public class Main {
@Test
public void test() throws Exception {
XmlMapper xmlMapper = new XmlMapper();
Note note = xmlMapper.readValue("""
<note xmlns="https://www.w3schools.com">
<extn first="primo" second="secondo" />
<subnote>
<extn third="pluto" fourth="pippo" />
</subnote>
<to>John</to>
<from>Jane</from>
<heading>Reminder</heading>
<body>Don't forget our meeting!</body>
</note>
""", Note.class);
System.out.println(xmlMapper.writeValueAsString(note));
}
}
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="https://www.w3schools.com"
xmlns="https://www.w3schools.com" elementFormDefault="qualified">
<xs:complexType name="personinfo">
<xs:sequence>
<xs:element name="extn">
<xs:complexType>
<xs:attribute name="third" type="xs:string" />
<xs:attribute name="fourth" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:schema>
<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>com.marcoaguzzi</groupId>
<artifactId>minimal-pom</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>minimal-pom</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>21</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-maven-plugin</artifactId>
<version>4.0.8</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.1</version>
</dependency>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="https://www.w3schools.com"
xmlns="https://www.w3schools.com"
elementFormDefault="qualified">
<xs:include schemaLocation="personinfo.xsd"/>
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="extn">
<xs:complexType>
<xs:attribute name="first" type="xs:string" />
<xs:attribute name="second" type="xs:string" />
</xs:complexType>
</xs:element>
<xs:element name="subnote" type="personinfo" />
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment