Home > Enterprise >  How do I remove the ns2 prefixes from XML elements using JAXB 3 and a NamespacePrefixMapper?
How do I remove the ns2 prefixes from XML elements using JAXB 3 and a NamespacePrefixMapper?

Time:02-23

I want to generate a POM file using JAXB 3. I downloaded the XSD https://maven.apache.org/xsd/maven-4.0.0.xsd into src/main/xsd. I generated the model classes using the JAXB maven plugin.

The POM file of the project is :

<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>

The main class used to generate the sample POM is :

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);
    }
}

My issue is, the output contains undesired ns2 prefixes. I didn't figure out how to tell the marshaller that the generated model uses the default namespace and not a namespace named 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>

CodePudding user response:

I forgot to provide the namespace to the QName :

new QName("http://maven.apache.org/POM/4.0.0", "project")

And now the result looks good :

<?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>

There's no need for a prefix mapper, so the code can be simplified:

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);
    }
}
  • Related