JAXB 解析xml文件出现的错误(模拟IOC模式) 本帖最后由 pf711 于 2014-03-11 21:41:46 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼主的在javabean上的jaxb注解有问题。只要对javabean的jaxb注解修改即可//Beans@XmlRootElement@XmlAccessorType(XmlAccessType.NONE)public class Beans {@XmlElementRef//用这个注解会连接到Bean类上 private List<Bean> beans; public List<Bean> getBeans() { return beans; } public void setBeans(List<Bean> beans) { this.beans = beans; }}//Bean@XmlRootElement@XmlAccessorType(XmlAccessType.NONE)public class Bean { @XmlAttribute private String id; @XmlAttribute private String classname; @XmlElementRef//用这个注解会连接到Property类上 private List<Property> properties; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getClassname() { return classname; } public void setClassname(String classname) { this.classname = classname; } public List<Property> getProperties() { return properties; } public void setProperties(List<Property> properties) { this.properties = properties; }}//Property@XmlRootElement@XmlAccessorType(XmlAccessType.NONE)public class Property { @XmlAttribute private String name; @XmlAttribute private String reference; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getReference() { return reference; } public void setReference(String reference) { this.reference = reference; }}//测试代码 JAXBContext context=JAXBContext.newInstance(Beans.class); Unmarshaller unmarshaller=context.createUnmarshaller(); Beans beans=(Beans) unmarshaller.unmarshal(new File("楼主的xml文件")); List<Bean> list=beans.getBeans(); for(Bean bean:list){ System.out.println(bean.getId()+":"+bean.getClassname()); List<Property> properties=bean.getProperties(); if(properties!=null){ for(Property property : properties){ System.out.println(property.getName()+":"+property.getReference()); } } } 第一眼看见代码是,我觉得你的impl中的l是不是写成1了... 通常注解写在get方法或属性上或属性上,set上倒是第一次听说 求正则 Spring 事务回滚问题 声明式事务回滚失败 我的mysql安装的时候用的utf8编码,在命令行执行insert语句时插不进中文 JAVAMAIL做邮件问题!! 关于jndi使用,急啊 spring框架用 burlap协议实现服务 本地的servlet可以访问其他机器上的servlet吗? Struts下拉框的问题,在ActionForm错误返回的时候出错了 请教下jquery easyui 表格url 是jsp文件如何返回数据 小弟有个问题~ 请高手说说 log4j并发写日志 ResourceBundle 读取本地文件
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Beans {
@XmlElementRef//用这个注解会连接到Bean类上
private List<Bean> beans; public List<Bean> getBeans() {
return beans;
} public void setBeans(List<Bean> beans) {
this.beans = beans;
}
}
//Bean
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Bean {
@XmlAttribute
private String id;
@XmlAttribute
private String classname;
@XmlElementRef//用这个注解会连接到Property类上
private List<Property> properties;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClassname() {
return classname;
}
public void setClassname(String classname) {
this.classname = classname;
}
public List<Property> getProperties() {
return properties;
}
public void setProperties(List<Property> properties) {
this.properties = properties;
}
}
//Property
@XmlRootElement
@XmlAccessorType(XmlAccessType.NONE)
public class Property {
@XmlAttribute
private String name;
@XmlAttribute
private String reference;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getReference() {
return reference;
}
public void setReference(String reference) {
this.reference = reference;
}
}//测试代码 JAXBContext context=JAXBContext.newInstance(Beans.class);
Unmarshaller unmarshaller=context.createUnmarshaller();
Beans beans=(Beans) unmarshaller.unmarshal(new File("楼主的xml文件"));
List<Bean> list=beans.getBeans();
for(Bean bean:list){
System.out.println(bean.getId()+":"+bean.getClassname());
List<Property> properties=bean.getProperties();
if(properties!=null){
for(Property property : properties){
System.out.println(property.getName()+":"+property.getReference());
}
}
}