Помогите пожалуйста. У меня есть следующие фрагменты:
Счет. java
@XmlRootElement(name="Account", namespace = "http://www.example.com/bank")
@XmlType(name = "Account", propOrder = {"name", "country", "type", "depositor", "amountOnDeposit", "profitability", "timeConstraints"})
@XmlAccessorType(XmlAccessType.FIELD)
public class Account {
@XmlAttribute(required = true)
private Integer id;
@XmlElement(required = true, name = "Name")
private String name;
@XmlElement(required = true, name = "Country")
private String country;
@XmlElement(required = true, name = "Type")
private String type;
@XmlElement(required = true, name = "Depositor")
private String depositor;
@XmlElement(required = true, name = "Amount-on-deposit")
private Double amountOnDeposit;
@XmlElement(required = true, name = "Profitability")
private Double profitability;
@XmlElement(required = true, name = "Time-constraints")
@XmlJavaTypeAdapter(DateAdapter.class)
private Date timeConstraints;
public Account(Integer id, String name, String country, String type, String depositor, Double amountOnDeposit, Double profitability, Date timeConstraints) {
this.id = id;
this.name = name;
this.country = country;
this.type = type;
this.depositor = depositor;
this.amountOnDeposit = amountOnDeposit;
this.profitability = profitability;
this.timeConstraints = timeConstraints;
}
public Account() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getDepositor() {
return depositor;
}
public void setDepositor(String depositor) {
this.depositor = depositor;
}
public Double getAmountOnDeposit() {
return amountOnDeposit;
}
public void setAmountOnDeposit(Double amountOnDeposit) {
this.amountOnDeposit = amountOnDeposit;
}
public Double getProfitability() {
return profitability;
}
public void setProfitability(Double profitability) {
this.profitability = profitability;
}
public Date getTimeConstraints() {
return timeConstraints;
}
public void setTimeConstraints(Date timeConstraints) {
this.timeConstraints = timeConstraints;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", country='" + country + '\'' +
", type='" + type + '\'' +
", depositor='" + depositor + '\'' +
", amountOnDeposit=" + amountOnDeposit +
", profitability=" + profitability +
", timeConstraints=" + timeConstraints +
'}';
}
}
Банк. java
@XmlRootElement(name="Bank", namespace = "http://www.example.com/bank")
@XmlType(name="Bank")
@XmlAccessorType(XmlAccessType.FIELD)
public class Bank {
@XmlElement(required = true, name = "Account")
private List<Account> accounts = new ArrayList<>();
public Bank(List<Account> accounts) {
this.accounts = accounts;
}
public Bank() {
}
public List<Account> getAccounts() {
return accounts;
}
public void setAccounts(List<Account> accounts) {
this.accounts = accounts;
}
public void addAccount(Account account) {
if (account != null && accounts != null) {
this.accounts.add(account);
}
}
@Override
public String toString() {
StringBuilder result = new StringBuilder();
result.append("[");
for (Account account : this.accounts) {
result.append(account.toString() + "\n");
}
result.append("]");
return result.toString();
}
}
банк. xml
<?xml version="1.0" encoding="UTF-8"?>
<Bank xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.example.com/bank"
xsi:schemaLocation="http://www.example.com/bank bank.xsd">
<Account id="1020">
<Name>NewBank</Name>
<Country>Ireland</Country>
<Type>Time</Type>
<Depositor>Sam Tailor</Depositor>
<Amount-on-deposit>20000.0</Amount-on-deposit>
<Profitability>10.5</Profitability>
<Time-constraints>2022-04-12</Time-constraints>
</Account>
<Account id="5000">
<Name>SwisBank</Name>
<Country>Switzerland</Country>
<Type>Time</Type>
<Depositor>Tray Olderson</Depositor>
<Amount-on-deposit>50000.0</Amount-on-deposit>
<Profitability>11.5</Profitability>
<Time-constraints>2025-10-15</Time-constraints>
</Account>
<Account id="2500">
<Name>Bank1000</Name>
<Country>France</Country>
<Type>Transactional</Type>
<Depositor>Helena Urwill</Depositor>
<Amount-on-deposit>45000.0</Amount-on-deposit>
<Profitability>10.4</Profitability>
<Time-constraints>2027-01-25</Time-constraints>
</Account>
<Account id="2700">
<Name>Bank1000</Name>
<Country>France</Country>
<Type>Transactional</Type>
<Depositor>Safo de Olaf</Depositor>
<Amount-on-deposit>20000.0</Amount-on-deposit>
<Profitability>10.0</Profitability>
<Time-constraints>2024-12-11</Time-constraints>
</Account>
<Account id="2800">
<Name>Bank1000</Name>
<Country>France</Country>
<Type>On-demand</Type>
<Depositor>Mishon Braul</Depositor>
<Amount-on-deposit>25000.0</Amount-on-deposit>
<Profitability>12.5</Profitability>
<Time-constraints>2025-08-10</Time-constraints>
</Account>
<Account id="2020">
<Name>NewBank</Name>
<Country>Ireland</Country>
<Type>Transactional</Type>
<Depositor>Nite Oswald</Depositor>
<Amount-on-deposit>42000.0</Amount-on-deposit>
<Profitability>10.2</Profitability>
<Time-constraints>2025-02-17</Time-constraints>
</Account>
<Account id="151005">
<Name>Bank "Tovarish"</Name>
<Country>Russia</Country>
<Type>On-demand</Type>
<Depositor>Petr Kamenshikov</Depositor>
<Amount-on-deposit>20000.0</Amount-on-deposit>
<Profitability>11.8</Profitability>
<Time-constraints>2022-08-25</Time-constraints>
</Account>
<Account id="254181">
<Name>Bank "Tovarish"</Name>
<Country>Russia</Country>
<Type>Time</Type>
<Depositor>Olga Nabokova</Depositor>
<Amount-on-deposit>70000.0</Amount-on-deposit>
<Profitability>14.5</Profitability>
<Time-constraints>2024-05-21</Time-constraints>
</Account>
<Account id="7000">
<Name>SwisBank</Name>
<Country>Switzerland</Country>
<Type>Metal</Type>
<Depositor>Gabriel Fynn</Depositor>
<Amount-on-deposit>55000.0</Amount-on-deposit>
<Profitability>12.5</Profitability>
<Time-constraints>2022-10-08</Time-constraints>
</Account>
<Account id="8000">
<Name>SwisBank</Name>
<Country>Switzerland</Country>
<Type>Time</Type>
<Depositor>Corsin David</Depositor>
<Amount-on-deposit>41000.0</Amount-on-deposit>
<Profitability>10.7</Profitability>
<Time-constraints>2024-11-01</Time-constraints>
</Account>
</Bank>
bank.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.com/bank"
xmlns:tns="http://www.example.com/bank"
elementFormDefault="qualified">
<element name="Bank">
<complexType>
<sequence>
<element name="Account"
type="tns:Account"
maxOccurs="unbounded" />
</sequence>
</complexType>
</element>
<complexType name="Account">
<sequence>
<element name="Name" type="string"/>
<element name="Country" type="string"/>
<element name="Type" type="string">
<simpleType>
<restriction base="string">
<enumeration value="On-demand"/>
<enumeration value="Time"/>
<enumeration value="Transactional"/>
<enumeration value="Saving"/>
<enumeration value="Metal"/>
</restriction>
</simpleType>
</element>
<element name="Depositor" type="string">
<simpleType>
<restriction base="string">
<pattern value="[A-Z][a-z]{1,31}((\s[A-Z][a-z]{1,31})|(\s[a-z]{2,32})){1,14}"/>
</restriction>
</simpleType>
</element>
<element name="Amount-on-deposit" type="decimal"/>
<element name="Profitability" type="decimal"/>
<element name="Time-constraints" type="date"/>
</sequence>
<attribute name="id" type="positiveInteger" use="required" />
</complexType>
</schema>
unmarshallMain. java
public class unmarshallMain {
public static void main(String[] args) {
try {
JAXBContext jc = JAXBContext.newInstance(Bank.class);
Unmarshaller u = jc.createUnmarshaller();
FileReader reader = new FileReader("src/main/java/part/two/chapter/fourteen/bank.xml");
Bank bank = (Bank) u.unmarshal(reader);
System.out.println(bank.toString());
} catch (JAXBException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Итак, проблема в том, что sysout печатает "[]
". Unmarshaller возвращает пустой объект. Если я удаляю схему из bank.xml
и пространства имен из @XmlRootElement
, она возвращает ожидаемый результат (sysout печатает девять объектов из xml). Итак, что-то не так с пространствами имен, я полагаю.
Кроме того, я попытался объявить отдельный файл package-info.java
с аннотацией @XmlSchema
, но результат тот же.
Кто-нибудь знает, что происходит? Спасибо за вашу помощь