Wednesday, March 14, 2012

how to add namespace Prefix to all the elements of JAX-WS webservice output

how to add namespace Prefix to all the elements of JAX-WS webservice output

This post will explain, how to add namespace Prefix to all the elements of JAX-WS webservice output

Sometimes we may required to have the namespace prefix (ex.ns1) defined for all the elments of the JAX-WS webservice output.

The default annotation of the package-info.java class looks like below

@javax.xml.bind.annotation.XmlSchema(namespace = "PPDS:OPIRequest", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.reuters.eai.types;

This case only the parent element will be have the namespace prefix but not the child elements.

eg.

<ns1:Employee ns1="PPDS:OPIRequest">
<name></name>
<number></number>
</ns1:Employee>

To have the namespace prefix in all the output elements,modify the above annotaion in the package-info.java class as mentioned below.

@XmlSchema( namespace = "PPDS:OPIRequest", elementFormDefault = XmlNsForm.QUALIFIED, xmlns={@XmlNs(prefix="ns3", namespaceURI="PPDS:OPIRequest")})
package com.reuters.eai.types;
import javax.xml.bind.annotation.*;

e.g

<ns1:Employee ns1="PPDS:OPIRequest">
<ns1:name></ns1:name>
<ns1:number></ns1:number>
</ns1:Employee>


4 comments:

  1. How do you change the prefix of a different package, aka namespace?

    ReplyDelete
  2. Create a package-info.java file in the package and specifiy the package name and the namespace accordingly.

    @javax.xml.bind.annotation.XmlSchema(namespace = "PPDS:OPIRequest", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
    package com.reuters.eai.types;

    Regards
    Albin I

    ReplyDelete
  3. The soap xml did not post to the comment, thinks it html I guess

    ReplyDelete
  4. ns3:yearlyReturnsAsOfDate
    dayOfMonth>30/dayOfMonth
    month>6/month
    year>2014/year

    ReplyDelete