35

I have generated Java classes from XSD, all works fine from a unmarshalling point of view.

However, when I marshall from JAXB classes I get the following:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<message xmlns="http://poc.cmc.com/ScreenLayout">
    <Data>
        <Type>Sample</Type>
     . . .
</message>

But I need

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns0:message xmlns:ns0="http://poc.cmc.com/ScreenLayout">
    <ns0:Data>
        <ns0:Type>Sample</ns0:Type>
    . . .

how can I control that from Java?

Thanks a lot

Zombies
  • 25,039
  • 43
  • 140
  • 225
Arthur .
  • 351
  • 1
  • 3
  • 3
  • 2
    The first listing is perfectly valid; it has the default namespace set to "http: //poc.cmc.com/ScreenLayout". Why do you need a namespace prefix? – perp Jun 09 '11 at 14:14
  • 7
    Hi, because the consuming application is very dumb and needs the prefix, and we have no control over it – Arthur . Jun 09 '11 at 14:21

4 Answers4

44

You can use the @XmlSchema annotation on a package-info class to assign a prefix to the namespace:

@XmlSchema(
    namespace = "http://poc.cmc.com/ScreenLayout",
    elementFormDefault = XmlNsForm.QUALIFIED,
    xmlns={@XmlNs(prefix="ns0", namespaceURI="http://poc.cmc.com/ScreenLayout")})    
package your.package;


import javax.xml.bind.annotation.*;
vbence
  • 20,084
  • 9
  • 69
  • 118
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • 2
    Didn't test it, but this would be nice. Unfortunately the java files are generated, so you'd need you build process to place this custom file in the same place as the generated files... – f1sh Jun 09 '11 at 14:42
  • What I don't get is there are nearly 20 package-info classes in my project. Which one do I need to add @XmlNs info? – hellzone Oct 30 '13 at 15:46
  • @hellzone - The following link may help: http://blog.bdoughan.com/2011/11/jaxb-and-namespace-prefixes.html – bdoughan Oct 30 '13 at 15:53
  • @blaise I followed this approach in my webservices. We use cxf2.1.7 with jaxb 2.1.7 in websphere8.5. But I get prefix 'a' instead of ns2. How can I get back to ns2? The same application and same jars are working fine and request is properly generated on websphere 7. This issue occurs only with websphere8.5. Any pointers pls? – San May 09 '18 at 04:19
  • Thanks, it worked for me! – alphamz Feb 07 '22 at 17:46
9

Cant post this as a comment!

because the consuming application is very dumb and needs the prefix

In that case the dumb application is not really consuming xml. Take a look at this link http://bdoughan.blogspot.com/2010/08/jaxb-namespaces.html and play with the namespace options. Specifically

@XmlSchema (
   xmlns = {
         @javax.xml.bind.annotation.XmlNs(prefix = "ns1", namespaceURI="http:test"),
         @javax.xml.bind.annotation.XmlNs(prefix = "xsd", namespaceURI="http:www.w3.org2001XMLSchema")
   },
   namespace = "http:test",
   elementFormDefault = XmlNsForm.UNQUALIFIED,
   attributeFormDefault = XmlNsForm.UNSET
)

used in a package-info.java file.

@XmlType(namespace="http://www.example.org/type")

Used on a class declaration

@XmlElement(namespace="http://www.example.org/property")

Used on a property.

Some combination or only one of these options may give you what you want. However you should understand that you're fighting an uphill battle when you move from valid xml to xml that must contain a specific namespace prefix on all elements.

Heathen
  • 623
  • 5
  • 13
2

According to XML spec both xml's are the same, as xmlns="" defines default namespace which applies to current and all child elements. XML parsers should give you the same DOM or SAX in both cases

Sergey Aslanov
  • 2,397
  • 15
  • 16
  • Ok,Solved with package-info.java: – Arthur . Jun 09 '11 at 14:42
  • @javax.xml.bind.annotation.XmlSchema(namespace = "http://poc.cmc.com/ScreenLayout", xmlns = {@XmlNs(namespaceURI = "http://poc.cmc.com/ScreenLayout", prefix = "ns1" )}, elementFormDefault = XmlNsForm.QUALIFIED) – Arthur . Jun 09 '11 at 14:42
2

I did the following steps and everything works. Basically I've compiled form the wsdl or whatever schema file you have.

  1. Download from https://repo1.maven.org/maven2/com/sun/xml/bind/jaxb-ri/ (I've used version 2.3.4)
  2. With the following command:
xjc.sh -wsdl -npa -mark-generated -d src/main/java -p your.package.hierarchy src/main/resources/wsdl/*

I had wsdl files and that's the reason for the first flag, the -npa is to add the namespace information in the annotation rather than on a package-info.java since for some reason that wasn't working for me.

Dharman
  • 30,962
  • 25
  • 85
  • 135