将java对象解析为xml文件时缺少专有名称
我想返回新的.xml文件,其中包含基于源xml文件的已验证列表。如何使用Jackson xmlMapper以正确的方式为帐户列表重写详细信息的名称 我想从.xml文件(Accounts details)中读取值,并将其转换为java对象。我需要把这些单一的账户归类到列表中。然后,我使用一些验证器验证这些帐户,然后我需要将检查过的帐户列表返回到新的xml文件中 名称“LinkedList”显示,因为我在服务类中使用它来重写列表 我在下面给你我的代码 有人能帮我吗 谢谢 源XML文件:将java对象解析为xml文件时缺少专有名称,java,xml,xml-parsing,jackson,Java,Xml,Xml Parsing,Jackson,我想返回新的.xml文件,其中包含基于源xml文件的已验证列表。如何使用Jackson xmlMapper以正确的方式为帐户列表重写详细信息的名称 我想从.xml文件(Accounts details)中读取值,并将其转换为java对象。我需要把这些单一的账户归类到列表中。然后,我使用一些验证器验证这些帐户,然后我需要将检查过的帐户列表返回到新的xml文件中 名称“LinkedList”显示,因为我在服务类中使用它来重写列表 我在下面给你我的代码 有人能帮我吗 谢谢 源XML文件: <ac
<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>
客户名单:
@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() +
'}';
}
}
@JacksonXmlRootElement(localName=“accounts”)
@XmlAccessorType(XmlAccessType.FIELD)
公共最终类AccountList实现可序列化{
私有静态最终长serialVersionUID=9215731280802778862L;
@JacksonXmlElementWrapper(localName=“accountList”,UseWrapper=false)
私人名单;
公共帐户列表(){
}
这个地方的能手和二传手
@凌驾
公共字符串toString(){
返回“雇员{”+
“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;
}
}
private accountvalidator siml验证程序;
公共AccountServiceImpl(){
this.validators=新AccountValidatorSiml();
}
@凌驾
public List validateEverySingleAccount(List sourceAccountList){
List validatedList=新建LinkedList();
对于(帐户:sourceAccountList){
if(验证器。检查所有验证器(帐户))
validatedList.add(帐户);
}
返回validatedList;
}
@凌驾
公共列表sortValidatedAccountList(列表验证帐户列表){
List sortedList=新链接列表(validatedAccountList);
Comparator Comparator=(o1,o2)->o1.getName().compareTo(o2.getName());
排序列表。排序(比较器);
返回分类列表;
}
}
主要类别:
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
publicstaticvoidmain(字符串[]args)抛出IOException、XMLStreamException{
Parser Parser=新解析器();
AccountServiceImpl服务=新的AccountServiceImpl();
AccountList=parser.readFromXML();
List listOfAccounts=List.getAccountList();
List listOfValidatedAccounts=service.ValidateEverySingeCounts(listOfAccounts);
List listOfSortedAccounts=service.sortValidatedAccountList(listOfValidatedAccounts);
writeToXML(listOfSortedAccounts);
对象服务是我进行验证的类。如果是Nessery,我将粘贴它
最终生成的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>
名称1
印尼国家电力公司
123.45
2031-06-10
姓名2
印尼国家电力公司
85
2035-10-01
不幸的是,在处理过程中,程序缺少名称,这是:丢失
AccountList
包装列表
。我修复了您的解析器
类,该类使用Ja仅限ckson类。请参见以下示例:
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
}
<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>
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
}
<accounts>
<account iban="PL61109010140000071219812871">
<name>name2</name>
<currency>PLN</currency>
<balance>85.00</balance>
<closingDate>2035-10-01</closingDate>
</account>
</accounts>