简体   繁体   中英

Jaxb to xml binding?

I am using CXF soap webservices. I am also JAXB for binding. I have to return below XML response to clients.

               <orderElement type="service">
                       <elementAttribute name="serviceName">
                               <attributeValue>Collector</attributeValue>
                       </elementAttribute>
                </orderElement>

Here type="service" and name="serviceName" will never change. only attributeValue will change. To get above XML response, what are the properties/fields my JAXB class should have ?

@XmlRootElement
public class OrderType {
  private String type;
  private ElementAttribute elementAttribute;
  @XmlAttribute
  public String getType(){
    return type;
  }
  public void setType( String value ){
    type = value;
  }
  @XmlElement
  public ElementAttribute getElementAttribute(){
    return elementAttribute;
  }
  public void setElementAttribute( ElementAttribute value ){
    elementAttribute = value;
  }
}

public class ElementAttribute {
  private String name;
  private String attributeValue;
  @XmlAttribute
  public String getName(){
    return name;
  }
  public void setName( String value ){
    name = value;
  }
  @XmlElement
  public String getAttributeValue(){
    return attributeValue;
  }
  public void setAttributeValue( String value ){
    attributeValue = value;
  }
}

And to create and marshal:

void marshal() throws Exception {
  OrderType order = new OrderType();
  order.setType( "service" );
  ElementAttribute elattr = new ElementAttribute();
  order.setElementAttribute( elattr );
  elattr.setName( "serviceName" );
  elattr.setAttributeValue( "Collector" );
  JAXBContext jc = JAXBContext.newInstance( OrderType.class );
  Marshaller m = jc.createMarshaller();
  StringWriter sw = new StringWriter();
  m.marshal( order, sw );
  System.out.println( sw.toString() );
}

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