java xmlattribute_java – JAXB @XmlAttribute @XmlValue实例

谢谢Blaise Doughan.

但在挖了10多个主题后,我决定以相反的方式开始.

我创建了一个MARSHALLING我的数据(对象)的新测试.实际上,我认为我使用了TDD(测试驱动开发)方式.

所以,我用测试数据填充了我的对象并应用了编组(从DATA创建了XML)并且看到了我的.数据不正确.我也查看了其他主题(感谢这个Java/JAXB: Unmarshall Xml to specific subclass based on an attribute)并更正了我的数据结构

记得我想得到

1000

Big message

2013.03.14

现在我的数据是:

package net.regmaster.onlinenic.model.response.resdata;

import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlAttribute;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlRootElement;

import javax.xml.bind.annotation.XmlValue;

import org.eclipse.persistence.oxm.annotations.XmlCustomizer;

/**

* @author annik

*

*/

@XmlAccessorType(XmlAccessType.FIELD)

@XmlRootElement(name="data")

//@XmlCustomizer(ResDataCustomiser.class)

public class XmlData

{

@XmlAttribute(name="name")

private String name;

@XmlValue

private String value;

/** Getter.

* @return the name

*/

public String getName() {

return name;

}

/** Setter.

* @param name the name to set

*/

public void setName(String name) {

this.name = name;

}

/** Getter.

* @return the value

*/

public String getValue() {

return value;

}

/** Setter.

* @param value the value to set

*/

public void setValue(String value) {

this.value = value;

}

}

并且:

package net.regmaster.onlinenic.model.response.resdata;

import java.util.List;

import javax.xml.bind.annotation.XmlRootElement;

/**

* @author annik

*

*/

@XmlRootElement

public class ResData

{

private List data;

/**

* Getter.

*

* @return the data

*/

public List getData() {

return data;

}

/**

* Setter.

*

* @param data

* the data to set

*/

public void setData(List data) {

this.data = data;

}

}

并且:

package net.regmaster.onlinenic.model.response;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;

import javax.xml.bind.annotation.XmlAccessorType;

import javax.xml.bind.annotation.XmlElement;

import javax.xml.bind.annotation.XmlElements;

import javax.xml.bind.annotation.XmlRootElement;

import javax.xml.bind.annotation.XmlTransient;

import javax.xml.bind.annotation.XmlType;

import net.regmaster.onlinenic.enumtype.OnicEnumAction;

import net.regmaster.onlinenic.enumtype.OnicEnumCategory;

import net.regmaster.onlinenic.model.response.resdata.ResData;

import net.regmaster.onlinenic.model.response.resdata.XmlData;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

/**

* @author annik

*

*/

@XmlRootElement(name = "response")

//@XmlType( propOrder = { "category", "action", "code", "message"})

public class OnicGreetingResponse

{

private OnicEnumCategory category;

private OnicEnumAction action;

private Integer code;

private String message;

// private GreetingResData resData;

private ResData resData;

//

@XmlTransient

private Logger LOG = LoggerFactory.getLogger(getClass());

/**

* Getter.

*

* @return the category

*/

public OnicEnumCategory getCategory() {

return category;

}

/**

* Setter.

*

* @param category

* the category to set

*/

public void setCategoryEnum(OnicEnumCategory category) {

this.category = category;

}

@XmlElement

public void setCategory(String category) {

try {

this.category = OnicEnumCategory.getEnum(category);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

LOG.error(e.getMessage());

}

}

/**

* Getter.

*

* @return the action

*/

public OnicEnumAction getAction() {

return action;

}

/**

* Setter.

*

* @param action

* the action to set

*/

public void setActionEnum(OnicEnumAction action) {

this.action = action;

}

@XmlElement

public void setAction(String action) {

try {

this.action = OnicEnumAction.getEnum(action);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

LOG.error(e.getMessage());

}

}

/**

* Getter.

*

* @return the code

*/

@XmlElement

public Integer getCode() {

return code;

}

/**

* Setter.

*

* @param code

* the code to set

*/

public void setCode(Integer code) {

this.code = code;

}

/**

* Getter.

*

* @return the message

*/

@XmlElements(value={@XmlElement})

public String getMessage() {

return message;

}

/**

* Setter.

*

* @param message

* the message to set

*/

public void setMessage(String message) {

this.message = message;

}

/** Getter.

* @return the resData

*/

public ResData getResData() {

return resData;

}

/** Setter.

* @param resData the resData to set

*/

@XmlElement

public void setResData(ResData resData) {

this.resData = resData;

}

@Override

public String toString() {

return "category=" + category + ", action=" + action + ", code=" + code + ", msg=" + message

+ ", resData:" + resData.toString();

}

}

和vu-alja:

我明白了 !

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值