Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 873
ravindranath Avatar asked Nov 30 '25 04:11

ravindranath


1 Answers

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

  • http://blog.bdoughan.com/2010/07/xpath-based-mapping.html
  • http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
like image 151
bdoughan Avatar answered Dec 02 '25 16:12

bdoughan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!