[英]Error:(19, 21) java: incompatible types: java.lang.Object cannot be converted to item
I'm writing a program using Jaxb to convert an xml file into a java object.我正在编写一个程序,使用 Jaxb 将 xml 文件转换为 java ZA8CFDE6331BD59EB2666C96。 However, in my main class, I'm extending my item class to my items class to get access to the data stored in there, but it's telling me that it requires an object and it received an item instead. However, in my main class, I'm extending my item class to my items class to get access to the data stored in there, but it's telling me that it requires an object and it received an item instead. I'm a bit confused.我有点困惑。 When I build the file I get the error message in the title.当我构建文件时,我在标题中收到错误消息。
Here's my main class-- I'm getting the error in the for loop for item这是我的主要课程——我在 for 循环 for item中遇到错误
import java.io.*;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class main {
public static void main(String[] args) throws IOException, JAXBException {
File file = new File("items.xml");
JAXBContext context = JAXBContext.newInstance(items.class);
Unmarshaller un = context.createUnmarshaller();
items itemData = (items) un.unmarshal(file);
List items = itemData.getitem();
for(item e: items){
System.out.println("DataType : "+e.getDataType());
System.out.println("Name : "+e.getName());
System.out.println("Data : "+e.getData());
System.out.println("Group : "+e.getGroup());
System.out.println("--------------------------");
}
}
}
Here's my item class这是我的商品 class
import javax.xml.bind.annotation.XmlElement;
public class item {
private String dataType;
private String name;
private int data;
private String group;
@XmlElement
public String getDataType(){
return dataType;
}
public void setDataType(String dataType){
this.dataType = dataType;
}
@XmlElement
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
@XmlElement
public int getData(){
return data;
}
public void setData(int data){
this.data = data;
}
@XmlElement
public String getGroup(){
return group;
}
public void setGroup(String group){
this.group = group;
}
}
Here is my items class这是我的物品 class
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "items")
public class items {
private List<item> item;
@XmlElement(name = "item")
public List<item> getItem(){
return item;
}
public void setItem(List<item> itemList){
this.item = itemList;
}
}
Here is my xml file这是我的 xml 文件
<items>
<item>
<dataType>Number</dataType>
<name>Harry Potter 1</name>
<data>5</data>
<group>Movie</group>
</item>
<item>
<dataType>Number</dataType>
<name>Harry Potter 2</name>
<data>6</data>
<group>Movies</group>
<items>
<item>
<dataType>Number</dataType>
<name>Harry Potter 3</name>
<data>7</data>
<group>Books</group>
</item>
<item>
<dataType>Number</dataType>
<name>Harry Potter 4</name>
<data>8</data>
<group>Books</group>
</item>
</items>
</item>
</items>
change改变
List items = itemData.gettem();
to至
List<item> items = itemData.getItem();
Firstly, as answered by Anique Azhar, fix your compilation error from首先,正如 Anique Azhar 所回答的那样,修复您的编译错误
List items = itemData.getitem();
to至
List<item> items = itemData.getItem();
Secondly, as said by Abra in the question comment, The class name should be Capitalized.其次,正如 Abra 在问题评论中所说, class 名称应大写。
Then to handle the element <items>
nested in <item>
, add a field items
of class Items
to class Item
.然后处理嵌套在<item>
中的元素<items>
,将 class Items
的字段items
添加到 class Item
。
public class Item {
private String dataType;
private String name;
private int data;
private String group;
private Items items;
// getter and setter
}
My working code is available in Github .我的工作代码在Github中可用。 See the commit history for what I did based on your code.请参阅我根据您的代码所做的提交历史记录。
Your programming journey is long.你的编程之旅很长。 Keep on.继续。 Take some time to learn Git .花一些时间学习Git 。 Next time ask question on stackoverflow, better share your code in Github.下次在 stackoverflow 上提问,最好在 Github 中分享您的代码。 It make people locate the problem faster.它使人们更快地定位问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.