3
<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?

bluish
  • 26,356
  • 27
  • 122
  • 180
  • You can use @XmlTransient on Product if you do not want to marschall or unmarschall it. http://docs.oracle.com/javaee/5/api/javax/xml/bind/annotation/XmlTransient.html – FuryFart Jan 17 '14 at 06:46
  • I don't want to create a Product class , but i have to create a product element. – ravindranath Jan 17 '14 at 07:46
  • So you want to serialize a ProductInfo object without a Product field to a xml file that has a tag and vice versa? – FuryFart Jan 17 '14 at 08:01

1 Answers1

3

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

bdoughan
  • 147,609
  • 23
  • 300
  • 400