【问题标题】:Jaxb to xml binding?Jaxb 到 xml 绑定?
【发布时间】:2014-12-30 11:23:30
【问题描述】:

我正在使用 CXF 肥皂网络服务。我也是用于绑定的 JAXB。我必须将以下 XML 响应返回给客户端。

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

这里 type="service"name="serviceName" 永远不会改变。只有 attributeValue 会改变。 要获得上述 XML 响应,我的 JAXB 类应该具有哪些属性/字段

【问题讨论】:

    标签: java xml web-services jaxb


    【解决方案1】:
    @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;
      }
    }
    

    并创建和编组:

    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() );
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-03
      • 1970-01-01
      • 1970-01-01
      • 2012-05-10
      • 2011-09-29
      • 1970-01-01
      相关资源
      最近更新 更多