简体   繁体   中英

JAXB: marshal/unmarshal different XML elements to/from one class

Let's say, I have the following class:

@XmlRootElement
class Payment {
    @XmlElement
    int amount;
    @XmlElement
    Currency currency;

    @XmlElement
    IResponse response;
}

If response==null - the element is a "request", otherwise - the element is a "response". When a request, the element should be (un)marshaled to (from) the root element called PaymentRequest , when a response - to (from) PaymentResponse .

How can I configure such marshaling algorithm? If JAXB can't do it, maybe some other engine can?

Create and marshal the root element like this:

JAXBElement<Payment> jbe;
if( payment.getResponse() != null ){
    jbe = wrap( null, "PaymentResponse", payment );
} else {
    jbe = wrap( null, "PaymentRequest", payment );
}
m.marshal( jbe, sw );

with the simple helper method

<T> JAXBElement<T> wrap( String ns, String tag, T o ){
    QName qtag = new QName( ns, tag );
    Class<?> clazz = o.getClass();
    @SuppressWarnings( "unchecked" )
    JAXBElement<T> jbe = new JAXBElement( qtag, clazz, o );
    return jbe;
}

A simple way to make unmarshalling possible is to create two subclasses PaymentResponse and PaymentRequest which act as @XmlRootElements. The ObjectFactory contains

@XmlElementDecl(namespace = "", name = "PaymentRequest")
public JAXBElement<PaymentRequest> 
createPaymentRequest(PaymentRequest value) {
    return new JAXBElement<PaymentRequest>(_Payment_QNAME, PaymentRequest.class, null, value);
}
@XmlElementDecl(namespace = "", name = "PaymentResponse")
public JAXBElement<PaymentResponse> 
createPaymentResponse(PaymentResponse value) {
    return new JAXBElement<PaymentResponse>(_Payment_QNAME, PaymentResponse.class, null, value);
}

Unmarshalling:

JAXBContext jc = JAXBContext.newInstance( PACKAGE );
Unmarshaller m = jc.createUnmarshaller();
JAXBElement<?> tb = null;
try {
    Payment payment = readFrom( Payment.class );
} catch( Exception e  ){
}

and the method readFrom:

public <T> T readFrom( Class<T> type ) throws Exception {
    try {
        JAXBContext jc = JAXBContext.newInstance( PACKAGE );
        Unmarshaller u = jc.createUnmarshaller();
        JAXBElement<T> jbe = (JAXBElement<T>)u.unmarshal( new File( XMLIN ) );
        return type.cast( jbe.getValue() );
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

I've finally implemented the unmarshaling by intercepting StAX events. Here is the code:

JAXBContext jc = JAXBContext.newInstance(RootElement.class, A.class, B.class, C.class, D.class, E.class);
Unmarshaller unmarsh = jc.createUnmarshaller();
XMLStreamReader xs = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(etalonRs));
XMLStreamReader xd = new StreamReaderDelegate(xs) {
      public static final String ROOT_ELEMENT = "TestRoot";
      public static final int REPLACEABLE_LEVEL = 2;
      public final Collection<String> sufficesToDelete = Arrays.asList("Rq", "Rs");

      protected Stack<String> elementNamesStack = new Stack<>();
      protected Set<String> replaceableNames = new HashSet<>();

      @Override
      public String getLocalName() {
          String realName = super.getLocalName();
          if (replaceableNames.contains(realName) && elementNamesStack.size() == REPLACEABLE_LEVEL) {
              for (String suffix : sufficesToDelete) {
                  if (realName.endsWith(suffix)) {
                      return realName.substring(0, realName.lastIndexOf(suffix));
                  }
              }
          }
          return realName;
      }

      @Override
      public int next() throws XMLStreamException {
          final int eventCode = super.next();
         processLevel(eventCode);
          return eventCode;
      }

      @Override
      public int nextTag() throws XMLStreamException {
          final int eventCode = super.nextTag();
          processLevel(eventCode);
          return eventCode;
      }

      private void processLevel(int eventCode) {
          switch (eventCode) {
              case XMLStreamReader.START_ELEMENT:
                  final String origElementName = super.getLocalName();
                  if ((elementNamesStack.size() + 1) == REPLACEABLE_LEVEL && elementNamesStack.peek().equals(ROOT_ELEMENT))
                      replaceableNames.add(origElementName);
                  elementNamesStack.push(origElementName);
                  break;
              case XMLStreamReader.END_ELEMENT: 
                  assert(elementNamesStack.pop().equals(super.getLocalName()));
                  break;

          }
      }
  };

Object o = unmarsh.unmarshal(xd);

Here are my test classes. Yes, the real structure in production is more complicated - there are different "payments" and their elements are not in the root, so I've had to use the @XmlAnyElement annotation:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "TestRoot")
public static class RootElement {
    @XmlElement(name = "SomeDate")
    private Date dt = new Date();

    @XmlAnyElement(lax=true)
    private A a = new C();
}

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "TestA")
@XmlType
public static abstract class A {
    private int fld1 = 1;

    @XmlAnyElement(lax=true)
    @XmlElementWrapper
    protected List<Object> list = new ArrayList<>();
}

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "TestC")
public static class C extends A {
    private int fld2  = 3;
}

Marshaling can be implemented in the same manner, but you have to write your "StreamWriterDelegate" from scratch.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM