简体   繁体   中英

Generate XSD from XML using xmlbeans, inst2xsd and Maven

I have an XML file I want to generate an XSD schema from, using xmlbeans , specifically inst2xsd . I'd like to package the script so it can be run via Maven. I could not find any documentation how to run inst2xsd when installing xmlbeans using Maven. This is my pom.xml so far:

<project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>de.wolkenarchitekt</groupId>
    <artifactId>xml-to-xsd</artifactId>
    <version>1</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>3.1.0</version>
        </dependency>
    </dependencies>
</project>

Installing this via mvn install works. Just for the reference - not important for the answer - I'm building it via Docker, so I'm using OpenJDK14:

FROM maven:3.6.3-openjdk-14-slim
RUN mkdir -p /opt/workspace
WORKDIR /opt/workspace
COPY pom.xml .
RUN mvn install

Now how do I run the executable for inst2xsd after installing xmlbeans via Maven?

You can use the Exec Maven Plugin to invoke the class Inst2Xsd . This class is the one actually called from the inst2xsd shell script.

If you do not need xmlbeans in your project - once your XSD is generated - you can event define this dependency only for that task.

Consider the following XML document:

<?xml version="1.0" encoding="UTF-8" ?>
<breakfast_menu>
  <food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description>
    <calories>650</calories>
  </food>
  <food>
    <name>Strawberry Belgian Waffles</name>
    <price>$7.95</price>
    <description>Light Belgian waffles covered with strawberries and whipped cream</description>
    <calories>900</calories>
  </food>
  <food>
    <name>Berry-Berry Belgian Waffles</name>
    <price>$8.95</price>
    <description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
    <calories>900</calories>
  </food>
  <food>
    <name>French Toast</name>
    <price>$4.50</price>
    <description>Thick slices made from our homemade sourdough bread</description>
    <calories>600</calories>
  </food>
  <food>
    <name>Homestyle Breakfast</name>
    <price>$6.95</price>
    <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
    <calories>950</calories>
  </food>
</breakfast_menu>

In the example we will name it food-menu.xml and save it in src/main/resources .

You can generate the XML schema as follows (the following example is derived from the code that you can find in the plugin documentation ):

<project>
  <!-- ... -->
    <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>3.0.0</version>
        <executions>
          <execution>
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <includeProjectDependencies>false</includeProjectDependencies>
          <includePluginDependencies>true</includePluginDependencies>
          <mainClass>org.apache.xmlbeans.impl.inst2xsd.Inst2Xsd</mainClass>
          <arguments>
            <!-- Add as many arguments as you need -->
            <argument>-outDir</argument>
            <argument>${project.build.outputDirectory}</argument>
            <argument>-validate</argument>
            <argument>${project.basedir}/src/main/resources/food-menu.xml</argument>
          </arguments>
        </configuration>
        <dependencies>
          <dependency>
            <groupId>org.apache.xmlbeans</groupId>
            <artifactId>xmlbeans</artifactId>
            <version>3.1.0</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
  <!-- ... -->
</project>

Just run mvn exec:java from your terminal or command line and the schema will be generated according to the arguments passed to Inst2Xsd .

The use of docker should not be a problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM