ravindranath
ravindranath

Reputation: 51

JAXB with nested elements

<productInfo>
  <product>
    <productPrice>
      <price></price>
    </productPrice>
  </product>
<productInfo>

To create an XML like the above I am creating classes for productInfo, product, productPrice and then I am "unmarshalling" using JAXB annotations.

Is there any way to unmarshall without creating product class?

Upvotes: 3

Views: 1296

Answers (1)

bdoughan
bdoughan

Reputation: 149047

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

You could use the @XmlPath extension in the MOXy implementation of JAXB to map this use case.

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;

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

    @XmlPath("product/productPrice/price/text()")
    private double price;

}

For More Information

Upvotes: 3

Related Questions