我想基于源xml文件返回带有经过验证的列表的新.xml文件。如何使用Jackson xmlMapper为帐户列表以正确的方式重写详细信息的名称?
我想从.xml文件(帐户详细信息)中读取值,并将其转换为Java对象。我需要将那些单一会计分组到列表中。然后,我使用一些验证器来验证那些帐户,然后我需要将已检查的帐户列表返回到新的xml文件中。
显示名称“ LinkedList”,因为我在服务类中使用它来重写列表。
我在下面给你我的代码。
有人可以帮助我吗?
谢谢!
源XML文件:
<accounts>
<account iban="PL61109010140000071219812875">
<name>name1</name>
<currency>PLN</currency>
<balance>123.45</balance>
<closingDate>2031-06-10</closingDate>
</account>
<account iban="PL61109010140000071219812871">
<name>name2</name>
<currency>PLN</currency>
<balance>85.00</balance>
<closingDate>2035-10-01</closingDate>
</account>
</accounts>
PARSER:
public class Parser {
private ObjectMapper objectMapper;
public Parser() {
this.objectMapper = new XmlMapper();
}
public AccountList readFromXML() throws IOException {
return objectMapper.readValue(
readTextFromFile(), AccountList.class);
}
public void writeToXML(List<Account> account) throws IOException, XMLStreamException {
StringWriter stringWriter = new StringWriter();
XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory();
XMLStreamWriter sw = xmlOutputFactory.createXMLStreamWriter(stringWriter);
XmlMapper mapper = new XmlMapper();
objectMapper.writeValue(new
File("src/main/resources/SortedData.xml"), account);
sw.writeStartDocument();
}
private String readTextFromFile() throws IOException {
InputStream in = new FileInputStream("src/main/resources/SourceData.xml");
BufferedReader buff = new BufferedReader(new InputStreamReader(in));
String line = buff.readLine();
StringBuilder builder = new StringBuilder();
while(line != null){
builder.append(line).append("\n");
line = buff.readLine(); }
return builder.toString();
}
}
帐户类别:
public final class Account implements Serializable {
private static final long serialVersionUID = -4816735989159211337L;
@JacksonXmlProperty(localName = "iban", isAttribute = true)
private String accountIban;
@JacksonXmlProperty(localName = "name")
private String name;
@JacksonXmlProperty(localName = "currency")
private String currency;
@JacksonXmlProperty(localName = "balance")
private BigDecimal balance;
@JacksonXmlProperty(localName = "closingDate")
private String closingDate;
public Account() {
}
public Account(String accountIban, String name, String currency, BigDecimal balance, String closingDate) {
this.accountIban = accountIban;
this.name = name;
this.currency = currency;
this.balance = balance;
this.closingDate = closingDate;
}
(I HAVE BASIC GETTERS AND SETTERS IN THS PLACE, BUT I DIDN'T PASE IT)
@Override
public String toString() {
return "Account{" +
"accountNumber='" + accountIban + '\'' +
", name='" + name + '\'' +
", currency='" + currency + '\'' +
", balance=" + balance +
", closingDate=" + closingDate +
'}';
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Account account = (Account) o;
return Objects.equals(accountIban, account.accountIban) &&
Objects.equals(name, account.name) &&
Objects.equals(currency, account.currency) &&
Objects.equals(balance, account.balance) &&
Objects.equals(closingDate, account.closingDate);
}
}
帐户列表:
@JacksonXmlRootElement(localName = "accounts")
@XmlAccessorType(XmlAccessType.FIELD)
public final class AccountList implements Serializable {
private static final long serialVersionUID = 9215731280802778862L;
@JacksonXmlElementWrapper(localName = "accountList", useWrapping = false)
private List<Account> accountList;
public AccountList() {
}
GETTERS AND SETTERS IN THIS PLACE
@Override
public String toString() {
return "Employees{" +
"employees=" + accountList.toString() +
'}';
}
}
服务等级:
private AccountValidatorsImpl validators;
public AccountServiceImpl() {
this.validators = new AccountValidatorsImpl();
}
@Override
public List<Account> validateEverySingleAccount(List<Account> sourceAccountList) {
List<Account> validatedList = new LinkedList<>();
for (Account account: sourceAccountList) {
if(validators.checkAllValidators(account))
validatedList.add(account);
}
return validatedList;
}
@Override
public List<Account> sortValidatedAccountList(List<Account> validatedAccountList){
List<Account> sortedList = new LinkedList<>(validatedAccountList);
Comparator<Account> comparator = (o1, o2) -> o1.getName().compareTo(o2.getName());
sortedList.sort(comparator);
return sortedList;
}
}
主要类别:
public static void main(String[] args) throws IOException, XMLStreamException {
Parser parser = new Parser();
AccountServiceImpl service = new AccountServiceImpl();
AccountList list = parser.readFromXML();
List<Account> listOfAccounts = list.getAccountList();
List<Account> listOfValidatedAccounts = service.validateEverySingleAccount(listOfAccounts);
List<Account> listOfSortedAccounts = service.sortValidatedAccountList(listOfValidatedAccounts);
parser.writeToXML(listOfSortedAccounts);
OBJECT SERVICE IS CLASS WHERE I DO VALIDATION. IF IT WILL BE NESSECERY I WILL PASTE IT
最终生成的XML文件:
<LinkedList>
<item iban="PL61109010140000071219812875">
<name>name1</name>
<currency>PLN</currency>
<balance>123.45</balance>
<closingDate>2031-06-10</closingDate>
</item>
<item iban="PL61109010140000071219812871">
<name>name2</name>
<currency>PLN</currency>
<balance>85.00</balance>
<closingDate>2035-10-01</closingDate>
</item>
</LinkedList>
不幸的是,在处理过程中,程序缺少名称,它是:丢失<帐户>并返回
答案 0 :(得分:0)
您忘了用List<Account>
将AccountList
换行。我修复了您的Parser
类,该类仅使用XML
类来读写Jackson
。参见以下示例:
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
public class XmlMapperApp {
public static void main(String[] args) throws Exception {
File inputFile = new File("./resource/test.xml").getAbsoluteFile();
File outputFile = new File("./resource/output.xml").getAbsoluteFile();
Parser parser = new Parser();
List<Account> accounts = parser.readFromXML(inputFile).getAccountList();
accounts.remove(0);
parser.writeToXML(accounts, outputFile);
}
}
class Parser {
private XmlMapper xmlMapper;
public Parser() {
this.xmlMapper = new XmlMapper();
this.xmlMapper.enable(SerializationFeature.INDENT_OUTPUT);
}
public AccountList readFromXML(File xmlFile) throws IOException {
return xmlMapper.readValue(xmlFile, AccountList.class);
}
public void writeToXML(List<Account> accounts, File output) throws IOException {
xmlMapper.writeValue(output, new AccountList(accounts));
}
}
class Account implements Serializable {
private static final long serialVersionUID = -4816735989159211337L;
@JacksonXmlProperty(localName = "iban", isAttribute = true)
private String accountIban;
@JacksonXmlProperty(localName = "name")
private String name;
@JacksonXmlProperty(localName = "currency")
private String currency;
@JacksonXmlProperty(localName = "balance")
private BigDecimal balance;
@JacksonXmlProperty(localName = "closingDate")
private String closingDate;
public Account() {
}
public Account(String accountIban, String name, String currency, BigDecimal balance, String closingDate) {
this.accountIban = accountIban;
this.name = name;
this.currency = currency;
this.balance = balance;
this.closingDate = closingDate;
}
// getters, setters, toString
}
@JacksonXmlRootElement(localName = "accounts")
class AccountList implements Serializable {
private static final long serialVersionUID = 9215731280802778862L;
@JacksonXmlProperty(localName = "account")
@JacksonXmlElementWrapper(useWrapping = false)
private List<Account> accountList;
public AccountList() {
}
public AccountList(List<Account> accountList) {
this.accountList = accountList;
}
// getters, setters, toString
}
上面的代码反序列化XML
有效负载,删除0-index
项,并将其余项写入output.xml
文件,如下所示:
<accounts>
<account iban="PL61109010140000071219812871">
<name>name2</name>
<currency>PLN</currency>
<balance>85.00</balance>
<closingDate>2035-10-01</closingDate>
</account>
</accounts>