如何使用 JAXB 3 和 NamespacePrefixMapper 从 XML 元素中删除 ns2 前缀?
How do I remove the ns2 prefixes from XML elements using JAXB 3 and a NamespacePrefixMapper?
我想使用 JAXB 3 生成 POM 文件。我将 XSD https://maven.apache.org/xsd/maven-4.0.0.xsd 下载到 src/main/xsd
。
我使用 JAXB maven 插件生成了模型 classes。
项目的POM文件为:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.myproject</groupId>
<artifactId>test-maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.evolvedbinary.maven.mojohaus</groupId>
<artifactId>jaxb-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>org.apache.maven.pom</packageName>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
</project>
用于生成示例 POM 的主要 class 是:
import org.apache.maven.pom.Dependency;
import org.apache.maven.pom.Model;
import org.apache.maven.pom.Model.Dependencies;
import org.glassfish.jaxb.core.v2.WellKnownNamespace;
import org.glassfish.jaxb.runtime.marshaller.NamespacePrefixMapper;
import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBElement;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
public class Main {
public static void main(String[] args) throws JAXBException {
Model model = new Model();
model.setModelVersion("4.0.0");
model.setDependencies(new Dependencies());
Dependency dependency = new Dependency();
dependency.setGroupId("commons-io");
dependency.setArtifactId("commons-io");
dependency.setVersion("2.11.0");
model.getDependencies().getDependency().add(dependency);
JAXBElement<Model> elem = new JAXBElement<>(new QName("project"), Model.class, model);
JAXBContext context = JAXBContext.newInstance(Model.class);
Marshaller m = context.createMarshaller();
m.setProperty("org.glassfish.jaxb.namespacePrefixMapper", new NamespacePrefixMapper() {
@Override
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
return switch (namespaceUri) {
case XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI -> "xsi";
case XMLConstants.W3C_XML_SCHEMA_NS_URI -> "xs";
case WellKnownNamespace.XML_MIME_URI -> "xmime";
default -> "";
};
}
});
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
m.marshal(elem, System.out);
}
}
我的问题是,输出包含不需要的 ns2 前缀。我不知道如何告诉编组器生成的模型使用默认命名空间而不是名为 ns2 的命名空间。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<project xmlns:ns2="http://maven.apache.org/POM/4.0.0">
<ns2:modelVersion>4.0.0</ns2:modelVersion>
<ns2:dependencies>
<ns2:dependency>
<ns2:groupId>commons-io</ns2:groupId>
<ns2:artifactId>commons-io</ns2:artifactId>
<ns2:version>2.11.0</ns2:version>
</ns2:dependency>
</ns2:dependencies>
</project>
我忘记为 QName 提供命名空间:
new QName("http://maven.apache.org/POM/4.0.0", "project")
现在结果看起来不错:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
</project>
不需要前缀映射器,因此可以简化代码:
import org.apache.maven.pom.Dependency;
import org.apache.maven.pom.Model;
import org.apache.maven.pom.Model.Dependencies;
import javax.xml.namespace.QName;
import jakarta.xml.bind.JAXB;
import jakarta.xml.bind.JAXBElement;
public class Main {
public static void main(String[] args) {
Model model = new Model();
model.setModelVersion("4.0.0");
model.setDependencies(new Dependencies());
Dependency dependency = new Dependency();
dependency.setGroupId("commons-io");
dependency.setArtifactId("commons-io");
dependency.setVersion("2.11.0");
model.getDependencies().getDependency().add(dependency);
JAXBElement<Model> elem = new JAXBElement<>(new QName("http://maven.apache.org/POM/4.0.0", "project"), Model.class, model);
JAXB.marshal(elem, System.out);
}
}
我想使用 JAXB 3 生成 POM 文件。我将 XSD https://maven.apache.org/xsd/maven-4.0.0.xsd 下载到 src/main/xsd
。
我使用 JAXB maven 插件生成了模型 classes。
项目的POM文件为:
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.myproject</groupId>
<artifactId>test-maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.evolvedbinary.maven.mojohaus</groupId>
<artifactId>jaxb-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<id>xjc</id>
<goals>
<goal>xjc</goal>
</goals>
</execution>
</executions>
<configuration>
<packageName>org.apache.maven.pom</packageName>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
</project>
用于生成示例 POM 的主要 class 是:
import org.apache.maven.pom.Dependency;
import org.apache.maven.pom.Model;
import org.apache.maven.pom.Model.Dependencies;
import org.glassfish.jaxb.core.v2.WellKnownNamespace;
import org.glassfish.jaxb.runtime.marshaller.NamespacePrefixMapper;
import javax.xml.XMLConstants;
import javax.xml.namespace.QName;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBElement;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Marshaller;
public class Main {
public static void main(String[] args) throws JAXBException {
Model model = new Model();
model.setModelVersion("4.0.0");
model.setDependencies(new Dependencies());
Dependency dependency = new Dependency();
dependency.setGroupId("commons-io");
dependency.setArtifactId("commons-io");
dependency.setVersion("2.11.0");
model.getDependencies().getDependency().add(dependency);
JAXBElement<Model> elem = new JAXBElement<>(new QName("project"), Model.class, model);
JAXBContext context = JAXBContext.newInstance(Model.class);
Marshaller m = context.createMarshaller();
m.setProperty("org.glassfish.jaxb.namespacePrefixMapper", new NamespacePrefixMapper() {
@Override
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
return switch (namespaceUri) {
case XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI -> "xsi";
case XMLConstants.W3C_XML_SCHEMA_NS_URI -> "xs";
case WellKnownNamespace.XML_MIME_URI -> "xmime";
default -> "";
};
}
});
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
m.marshal(elem, System.out);
}
}
我的问题是,输出包含不需要的 ns2 前缀。我不知道如何告诉编组器生成的模型使用默认命名空间而不是名为 ns2 的命名空间。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<project xmlns:ns2="http://maven.apache.org/POM/4.0.0">
<ns2:modelVersion>4.0.0</ns2:modelVersion>
<ns2:dependencies>
<ns2:dependency>
<ns2:groupId>commons-io</ns2:groupId>
<ns2:artifactId>commons-io</ns2:artifactId>
<ns2:version>2.11.0</ns2:version>
</ns2:dependency>
</ns2:dependencies>
</project>
我忘记为 QName 提供命名空间:
new QName("http://maven.apache.org/POM/4.0.0", "project")
现在结果看起来不错:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
</dependencies>
</project>
不需要前缀映射器,因此可以简化代码:
import org.apache.maven.pom.Dependency;
import org.apache.maven.pom.Model;
import org.apache.maven.pom.Model.Dependencies;
import javax.xml.namespace.QName;
import jakarta.xml.bind.JAXB;
import jakarta.xml.bind.JAXBElement;
public class Main {
public static void main(String[] args) {
Model model = new Model();
model.setModelVersion("4.0.0");
model.setDependencies(new Dependencies());
Dependency dependency = new Dependency();
dependency.setGroupId("commons-io");
dependency.setArtifactId("commons-io");
dependency.setVersion("2.11.0");
model.getDependencies().getDependency().add(dependency);
JAXBElement<Model> elem = new JAXBElement<>(new QName("http://maven.apache.org/POM/4.0.0", "project"), Model.class, model);
JAXB.marshal(elem, System.out);
}
}