我正在使用JAXB将xml转换为对象。在做了一些教程后,我可以成功地将一个简单的xml(具有单个对象和唯一元素标记)转换为对象。然后我需要处理更复杂的xml,它有多个实例和一个父标记。我仍然使用类似的结构。但是我无法获得三个国家/地区对象的预期输出。我的代码出了什么问题?请帮忙。 IntelliJ IDE控制台输出是:
Countries@5e20a82a
Process finished with exit code 0
xml文件:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Countries>
<Country>
<Country_Name>Spain</Country_Name>
<Country_Capital>Madrid</Country_Capital>
<Country_Continent>Europe</Country_Continent>
</Country>
<Country>
<Country_Name>USA</Country_Name>
<Country_Capital>Washington</Country_Capital>
<Country_Continent>America</Country_Continent>
</Country>
<Country>
<Country_Name>Japan</Country_Name>
<Country_Capital>Tokyo</Country_Capital>
<Country_Continent>Asia</Country_Continent>
</Country>
</Countries>
Countries.java
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlType( propOrder = { "name", "capital", "foundation", "continent" , "population"} )
@XmlRootElement( name = "Countries" )
public class Countries {
private int population;
private String name;
private String capital;
private int importance;
private String foundation;
private String continent;
@XmlElement(name = "Country_Population")
public void setPopulation(int population) {
this.population = population;
}
@XmlElement(name = "Country_Name")
public void setName(String name) {
this.name = name;
}
@XmlElement(name = "Country_Capital")
public void setCapital(String capital) {
this.capital = capital;
}
@XmlAttribute(name = "importance", required = true)
public void setImportance(int importance) {
this.importance = importance;
}
@XmlElement(name = "Country_foundation")
public void setFoundation(String foundation) {
this.foundation = foundation;
}
@XmlElement(name = "Country_Continent")
public void setContinent(String continent) {
this.continent = continent;
}
}
CountryReader.java
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;
public class CountryReader {
public static void main(String[] args) throws JAXBException {
File file = new File("country.xml");
JAXBContext jaxbContext = JAXBContext.newInstance( Countries.class );
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Countries countres = (Countries)jaxbUnmarshaller.unmarshal( file );
System.out.println( countres );
}
}
答案 0 :(得分:0)
您应该将根元素和嵌套集合元素的映射分开。
所以你会有这样的事情:
@XmlRootElement( name = "Countries" )
public class Countries {
private List<Country> countries;
@XmlElement(name = "Country")
public void setCountries(List<Country> countries) {
this.countries = countries;
}
}
@XmlType( propOrder = { "name", "capital", "foundation", "continent" , "population"} )
@XmlRootElement( name = "Country" )
public class Country {
private int population;
private String name;
private String capital;
private int importance;
private String foundation;
private String continent;
@XmlElement(name = "Country_Population")
public void setPopulation(int population) {
this.population = population;
}
@XmlElement(name = "Country_Name")
public void setName(String name) {
this.name = name;
}
@XmlElement(name = "Country_Capital")
public void setCapital(String capital) {
this.capital = capital;
}
@XmlAttribute(name = "importance", required = true)
public void setImportance(int importance) {
this.importance = importance;
}
@XmlElement(name = "Country_foundation")
public void setFoundation(String foundation) {
this.foundation = foundation;
}
@XmlElement(name = "Country_Continent")
public void setContinent(String continent) {
this.continent = continent;
}
}
有关在JAXB中处理集合的更多信息,请参阅this博文。 @XmlElementWrapper
似乎适合您的任务,但我认为它不能直接用于根元素。