我有这个JAXB组件,我想以列表的形式获得:
@XmlRootElement(name = "payment")
@XmlAccessorType(XmlAccessType.FIELD)
public class Transaction {
@XmlElement(name = "transaction_types")
public TransactionTypes transactionTypes;
}
public class TransactionTypes {
@XmlElement(name = "transaction_type")
public String transaction_type;
@XmlAttribute
public String name;
public String getTransaction_type() {
return transaction_type;
}
public void setTransaction_type(String transaction_type) {
this.transaction_type = transaction_type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
XML结构:
<payment>
<transaction_types>
<transaction_type name="type1"/>
<transaction_type name="type2"/>
<transaction_type name="type3"/>
</transaction_types>
</payment>
问题是如何获取所有交易类型的列表?
您能告诉我一些如何修改JAXB对象吗?
编辑:
我尝试过
@XmlRootElement(name = "payment")
@XmlAccessorType(XmlAccessType.FIELD)
public class Transaction {
@XmlElementWrapper(name = "transaction_types")
public List<TransactionTypes> transactionTypes;
}
将保存列表的内部对象:
public class TransactionTypes {
@XmlElement(name = "transaction_type")
public String transaction_type;
@XmlAttribute
public String name;
public String getTransaction_type() {
return transaction_type;
}
public void setTransaction_type(String transaction_type) {
this.transaction_type = transaction_type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
答案 0 :(得分:1)
要获得所有transaction_type
的列表,我对您的代码做了一些修改。我介绍了一个新类TransactionList
,它将包含transaction_type
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "payment")
@XmlAccessorType(XmlAccessType.FIELD)
public class Transaction {
@XmlElement(name = "transaction_types")
public TransactionList transactionList;
public Transaction() {
super();
}
@Override
public String toString() {
return "Transaction [TransactionList=" + transactionList + "]";
}
}
TransactionList类
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class TransactionList {
@XmlElement(name = "transaction_type")
public List<TransactionType> transactionType;
public TransactionList(List<TransactionType> transactionTypes) {
transactionType = transactionTypes;
}
public TransactionList() {
super();
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (TransactionType transactionType : transactionType) {
sb.append(transactionType + "\n");
}
return sb.toString();
}
}
TransactionType类
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
@XmlAccessorType(XmlAccessType.FIELD)
public class TransactionType {
@XmlElement(name = "transaction_type")
private String transaction_type;
@XmlAttribute
private String name;
public TransactionType(String transaction_type, String name) {
this.transaction_type = transaction_type;
this.name = name;
}
public TransactionType() {
}
public String getTransaction_type() {
return transaction_type;
}
public void setTransaction_type(String transaction_type) {
this.transaction_type = transaction_type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "TransactionTypes [transaction_type=" + transaction_type + ", name=" + name + "]";
}
}
主要方法
public static void main(String[] args) throws Exception {
JAXBContext context = JAXBContext.newInstance(Transaction.class);
Unmarshaller um = context.createUnmarshaller();
Transaction transaction = (Transaction) um.unmarshal(new FileReader(FILE));
System.out.println(transaction);
}
您还可以通过直接将transactionType
表中的变量List
更改为Transaction
并为其分配XMLElementWrapper
批注来检查输出
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "payment")
@XmlAccessorType(XmlAccessType.FIELD)
public class Transaction {
@XmlElementWrapper(name = "transaction_types")
public List<TransactionType> transactionType;
public Transaction() {
}
}