使用 xmlbeans、inst2xsd 和 Maven 从 XML 生成 XSD
Generate XSD from XML using xmlbeans, inst2xsd and Maven
我有一个 XML 文件,我想使用 xmlbeans, specifically inst2xsd 从中生成一个 XSD 模式。我想打包脚本,以便它可以通过 Maven 运行。
在使用 Maven 安装 xmlbeans 时,我找不到任何关于如何 运行 inst2xsd 的文档。
这是我的 pom.xml
到目前为止:
<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>
通过 mvn install
安装这个可以。仅供参考 - 对答案不重要 - 我通过 Docker 构建它,所以我使用 OpenJDK14:
FROM maven:3.6.3-openjdk-14-slim
RUN mkdir -p /opt/workspace
WORKDIR /opt/workspace
COPY pom.xml .
RUN mvn install
现在如何在通过 Maven 安装 xmlbeans 后 运行 inst2xsd
的可执行文件?
您可以使用 Exec Maven Plugin 调用 class Inst2Xsd
。这个 class 实际上是从 inst2xsd
shell 脚本中调用的。
如果您的项目中不需要 xmlbeans
- 一旦您的 XSD 生成 - 您可以只为该任务定义此依赖项。
考虑以下 XML 文档:
<?xml version="1.0" encoding="UTF-8" ?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>.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>.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>.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>.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>
在示例中我们将其命名为food-menu.xml
并保存在src/main/resources
中。
您可以按如下方式生成 XML 架构(以下示例源自您可以在插件 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>
只需从您的终端或命令行 运行 mvn exec:java
即可根据传递给 Inst2Xsd
.
的参数生成架构
使用docker应该没有问题
我有一个 XML 文件,我想使用 xmlbeans, specifically inst2xsd 从中生成一个 XSD 模式。我想打包脚本,以便它可以通过 Maven 运行。
在使用 Maven 安装 xmlbeans 时,我找不到任何关于如何 运行 inst2xsd 的文档。
这是我的 pom.xml
到目前为止:
<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>
通过 mvn install
安装这个可以。仅供参考 - 对答案不重要 - 我通过 Docker 构建它,所以我使用 OpenJDK14:
FROM maven:3.6.3-openjdk-14-slim
RUN mkdir -p /opt/workspace
WORKDIR /opt/workspace
COPY pom.xml .
RUN mvn install
现在如何在通过 Maven 安装 xmlbeans 后 运行 inst2xsd
的可执行文件?
您可以使用 Exec Maven Plugin 调用 class Inst2Xsd
。这个 class 实际上是从 inst2xsd
shell 脚本中调用的。
如果您的项目中不需要 xmlbeans
- 一旦您的 XSD 生成 - 您可以只为该任务定义此依赖项。
考虑以下 XML 文档:
<?xml version="1.0" encoding="UTF-8" ?>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>.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>.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>.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>.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
<food>
<name>Homestyle Breakfast</name>
<price>.95</price>
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
<calories>950</calories>
</food>
</breakfast_menu>
在示例中我们将其命名为food-menu.xml
并保存在src/main/resources
中。
您可以按如下方式生成 XML 架构(以下示例源自您可以在插件 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>
只需从您的终端或命令行 运行 mvn exec:java
即可根据传递给 Inst2Xsd
.
使用docker应该没有问题