Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove ns2 as default namespace prefix

Tags:

java

xml

jaxb

I have a file that is printed with a default namespace. The elements are printed with a prefix of ns2, I need this to be removed, how it is with my code:

<ns2:foo xmlns:ns2="http://namespace" /> 

how I want it to be:

<foo xmlns="http://namespace" /> 

this is how I have coded it, something which as I see it should be enough for the ns2 to go away:

<?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"     xmlns:bar="http://namespace" targetNamespace="http://namespace"     elementFormDefault="qualified"> ... 

the generated package-info turns out like this:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://namespace",      elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED) package com.foo.bar; 

I create the file like this:

JAXBContext jaxbContext = JAXBContext.newInstance(generatedClassesPackage); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(new JAXBElement<Foo>(new QName("http://namespace", "Foo"), Foo.class, rootFoo), outputStream); 

generatedClassesPackage is the package where package-info.java and the elements are.

The Foo object is defined and has elements like this::

@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = {     "group" }) @XmlRootElement(name = "Foo") public class Foo {      @XmlElement(name = "Group", required = true)     protected List<Group> group; 

Is it something I have missed? or have I misunderstood how this works?

like image 789
Vegard Avatar asked May 16 '13 14:05

Vegard


People also ask

How do I remove namespace prefix?

Once a namespace prefix is created, it cannot be changed or deleted. The workaround is to move all your code to a new Developer Organization, where you can setup the desired Namespace Prefix.

How do I remove namespace prefix JAXB?

You can use the NamespacePrefixMapper extension to control the namespace prefixes for your use case. The same extension is supported by both the JAXB reference implementation and EclipseLink JAXB (MOXy).

How do I set namespace prefix in JAXB?

If you are using the default JAXB implementation provided with Java 6 or later, you can configure the namespace prefixes by extending the NamespacePrefixMapper and setting a property to tell the marshaller to use your extension.


2 Answers

All you need 2 do is when you open a new package select create package info in the package info add the following annotation or change it as needed

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.sitemaps.org/schemas/sitemap/0.9", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED, xmlns = { @javax.xml.bind.annotation.XmlNs(namespaceURI = "http://www.sitemaps.org/schemas/sitemap/0.9", prefix = "") }) 

This will remove the ns2 prefix

like image 106
yonia Avatar answered Sep 18 '22 20:09

yonia


Most likely you have multiple namespaces in the response. This will use the default convention of creating ns# namespace prefixes and one of them becomes the xmlns without a prefix. If you want to control this you can do the following:

NamespacePrefixMapper mapper = new NamespacePrefixMapper() {         public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {             if ("http://namespace".equals(namespaceUri) && !requirePrefix)                 return "";             return "ns";         }     };     marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", mapper);     marshaller.mashal.... 

This will set the http://namespace as the default xmlns always and use ns# for all other namespaces when marshalling. You can also give them more descriptive prefixes if you want.

like image 32
Daniel Moses Avatar answered Sep 19 '22 20:09

Daniel Moses