0
votes

I have a Root Class called Employee, which has two elements empid and name and another jaxb class called Address. Below is the sample snippet.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Request",propOrder = {
    "header",
    "body",
    "signature"
})
@XmlRootElement(name="Employee")
public class Employee
    implements Serializable
{

 @XmlElement(name = "Header", required = true)
    protected String empId;
    @XmlElement(name = "Body", required = true)
    protected String empName;
    @XmlElement(name = "Address", required = true)
    protected Address address;

.. setters and getters
}


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Address", propOrder = {
    "streetLine1",
    "streetLine2",
})
@XmlRootElement(name="Address",namespace= "http://www.w3.org/2000/09/xmldsig#")
public class Employee
    implements Serializable
{

    private final static long serialVersionUID = 100L;
    @XmlElement(name = "addressLine1", required = true)
    protected String addressLine1;
    @XmlElement(name = "addressLine2", required = true)
    protected String addressLine2;

//Setters and getters

}

Now when I generate the XML string with jaxb marshalling I want the expected result like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<Employee xmlns="http://www.test.com">

   <empId>124</empId>
   <empName>name</empName>

   <Address xmlns:ns2="http://www.w3.org/2000/09/xmldsig#">
       <ns2:streetLine1 Id="line1"/>
       <ns2:streetLine2 Id="Line2"/>
   </Address>
</Request>

Please suggest. Thanks in Advance.

1

1 Answers

0
votes

There are some problems with your JAXB classes, I think you have copy pasted and changed some names wrongly. The following elements inside @XMLType must be defined as @XMLElement.

@XmlType(name = "Request",propOrder = {
    "header",
    "body",
    "signature"
})

Anyways assuming the classes are right. You will need 2 changes to generate XML that has elements referred in different namespace.

  • Move the namespace to package level using @XMLSchema. i.e Add package-info.java at the package level to specify the namespaces.
  • Provide Address element with its own namespace in Employee class. Each element if not in parent namespace, must be overridden at this level.

package-info.java

@XmlSchema(
        namespace = "http://www.test.com",
        elementFormDefault = XmlNsForm.QUALIFIED)
package int1.d;

import javax.xml.bind.annotation.*;

Employee.java

package int1.d;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Request",propOrder = {
        "header",
        "body",
        "signature"
    })
@XmlRootElement(name="Employee")
public class Employee
    implements Serializable
{

    private static final long serialVersionUID = 8293193254658211943L;

    @XmlElement(name = "Header", required = true)
    protected String empId;
    @XmlElement(name = "Body", required = true)
    protected String empName;
    @XmlElement(name = "Address", namespace="http://www.w3.org/2000/09/xmldsig#", required = true )
    protected Address address;
    public String getEmpId() {
        return empId;
    }
    public void setEmpId(String empId) {
        this.empId = empId;
    }
    public String getEmpName() {
        return empName;
    }
    public void setEmpName(String empName) {
        this.empName = empName;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
}

package-info.java

@XmlSchema(
        namespace = "http://www.w3.org/2000/09/xmldsig#",
        elementFormDefault=XmlNsForm.QUALIFIED )    
package int1.d2;

import javax.xml.bind.annotation.*;

Address.java

package int1.d2;

import java.io.Serializable;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Address", propOrder = {
        "streetLine1",
        "streetLine2",
    })
@XmlRootElement(name="Address")
public class Address
    implements Serializable
{

    private final static long serialVersionUID = 100L;
    @XmlElement(name = "addressLine1", required = true)
    protected String addressLine1;
    @XmlElement(name = "addressLine2", required = true)
    protected String addressLine2;

    public String getAddressLine1() {
        return addressLine1;
    }
    public void setAddressLine1(String addressLine1) {
        this.addressLine1 = addressLine1;
    }
    public String getAddressLine2() {
        return addressLine2;
    }
    public void setAddressLine2(String addressLine2) {
        this.addressLine2 = addressLine2;
    }


}

output generated by JAXB

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Employee xmlns="http://www.test.com" xmlns:ns2="http://www.w3.org/2000/09/xmldsig#">
    <Header>124</Header>
    <Body>bae</Body>
    <ns2:Address>
        <ns2:addressLine1>line1</ns2:addressLine1>
        <ns2:addressLine2>line2</ns2:addressLine2>
    </ns2:Address>
</Employee>