1

I am using JAXB with annotations. I want the creditcard info to be displayed without the creditcardinfo node being displayed. FYI CreditCardInfo is an object of complex type.

@XmlRootElement
public Class Notification{
private String notifDate;
private CreditCardInfo ccInfo;
}

public Class CreditCardInfo{
private int ccNum;
private String expiryMonth;
}

Desired output

<notification>
<date>04/29/11</date>
<ccNum>3456</ccNum>
<expiry_month>November</expiry_month>
</notification>

Regards, -Anand

Anand
  • 1,791
  • 5
  • 23
  • 41
  • Hi even I am looking for something like mentioned within the answer. Have you able to figure out how to accomplish the same without `@XmlPath(".")`? As mentioned in by @bdoughan using the `create an intermediary object to map to your XML`? I am stuck with this issue for the last 10 days. I have posted the question here: https://stackoverflow.com/q/67648941/7584240 – BATMAN_2008 Jun 04 '21 at 08:50

1 Answers1

0

You could use the @XmlPath extension in EclipseLink JAXB (MOXy) to handle this use case. Note: I'm the MOXy lead.

Notification

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 org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Notification{

    @XmlElement(name="date")
    private String notifDate;

    @XmlPath(".")
    private CreditCardInfo ccInfo;

}

CreditCardInfo

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

@XmlAccessorType(XmlAccessType.FIELD)
public class CreditCardInfo{

    private int ccNum;

    @XmlElement(name="expiry_month")
    private String expiryMonth;

}

Demo

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Notification.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        Notification notification = (Notification) unmarshaller.unmarshal(new File("input.xml"));

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(notification, System.out);
    }

}

jaxb.properties

To use MOXy as your JAXB provider you need to add a file named jaxb.properties in the same package as your model classes with the following entry:

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Input/Output

<?xml version="1.0" encoding="UTF-8"?>
<notification>
   <date>04/29/11</date>
   <ccNum>3456</ccNum>
   <expiry_month>November</expiry_month>
</notification>

For More Information

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Blaise - just a quick question. Is there any other solution other than using Moxy? I mean I do not want to import a new library into my project. – Anand May 03 '11 at 18:21
  • @Anand Parthasarathy - Without MOXy you will need to create an intermediary object to map to your XML. Then you will need to copy data from that model to your desired model. – bdoughan May 03 '11 at 18:23