使用 JAXB,我想解组一个包含多个序列化对象的 xml 文档,如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<Users>
<User>
<firstName>first name value 1</firstName>
<lastName>last name value 1</lastName>
<account>
<expiration>expire 1</expiration>
<login>login 1</login>
</account>
</User>
<User>
<firstName>first name value 2</firstName>
<lastName>last name value 2</lastName>
<account>
<expiration>expire 2</expiration>
<login>login 2</login>
</account>
</User>
...
</Users>
事实是,我不想创建一个名为“Users”的新类,它包含一个用户元素列表(带有@XmlWrapper 注释)。
这是我必须转换为 xml 的 java 对象:
@Entity
@Table(name="USERS")
@XmlRootElement(name="User")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
@OneToOne(cascade={CascadeType.REMOVE}, mappedBy="user")
private Account account;
@XmlAttribute
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@XmlElement
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@XmlElement
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@XmlInverseReference(mappedBy="user")
@XmlElement
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
}
目前我可以只将一个 xml 解组到一个用户 java 对象。像这样 :
@Test
public void test2() {
try {
JAXBContext jc = JAXBContext.newInstance(User.class);
Unmarshaller u = jc.createUnmarshaller();
File f = new File("user.xml");
User element = (User) u.unmarshal(f);
System.out.println(
element.getAccount().getLogin()
);
} catch (JAXBException e) {
e.printStackTrace();
}
}
我想获得一个用户 java 列表实例,而不仅仅是一个用户实例。例如像这样:
List<User> elements = (List<User>) u.unmarshal(f);
我希望这是可能的,我想知道如何;)
非常感谢您的回复布莱斯。
我试着像你一样做,但我有错误:
java.lang.ClassCastException:com.sun.org.apache.xerces.internal.dom.ElementNSImpl 无法转换为 com.thales.momoko.ws.model.User
以下是我的代码的一些相关部分:
public class Tools<T> {
public List<T> getItems(Class<T> entityClass, String xmlLocation) {
try {
JAXBContext jc = JAXBContext.newInstance(Wrapper.class, entityClass.getClass());
Unmarshaller unmarshaller = jc.createUnmarshaller();
BufferedReader br = new BufferedReader(
new InputStreamReader(
this.getClass().getClassLoader().getResourceAsStream(xmlLocation)));
System.out.println(br.readLine());
Wrapper<T> wrapper = (Wrapper<T>) unmarshaller.unmarshal(new StreamSource(br), Wrapper.class).getValue();
System.out.println(wrapper);
return wrapper.getItems();
} catch (JAXBException ex) {
Logger.getLogger(Tools.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Tools.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
第一个 println 工作正常,因为它显示了 xml 文件的第一行:
<?xml version=....>
第二个 println 显示了解组的问题:
Wrapper{items=[[user:null],[user:null],[user:null],[user:null],[user:null],[user:null],[user:null],[user:空],[用户:空],[用户:空],[用户:空],[用户:空],[用户:空],[用户:空],[用户:空]]}
包装:
public class Wrapper<T> {
private List<T> items = new ArrayList<>();
@XmlAnyElement(lax=true)
public List<T> getItems() {
return items;
}
@Override
public String toString() {
return "Wrapper{" + "items=" + items + '}';
}
}
最后是 unmarshaller 的调用:
@PostConstruct
public void init() {
this.entityClass = User.class;
for (User user : (List<User>) new Tools<User>().getItems(User.class, "user.xml"))
System.out.println(user.getFirstName());
}
它在“for”指令的行中给了我一个错误。
你对这个错误有什么想法吗?
再次感谢!
编辑
解决方案 :
public class Tools<T> {
public static <T> List<T> getItems(Class<T> entityClass, String xmlLocation) {
try {
JAXBContext jc;
synchronized (JAXBContext.class) {
jc = JAXBContext.newInstance(Wrapper.class, entityClass);
}
Unmarshaller unmarshaller = jc.createUnmarshaller();
BufferedReader br = new BufferedReader(
new InputStreamReader(
Import.class.getClassLoader().getResourceAsStream(xmlLocation)));
Wrapper<T> wrapper = (Wrapper<T>) unmarshaller.unmarshal(new StreamSource(br), Wrapper.class).getValue();
return wrapper.getItems();
} catch (JAXBException ex) {
Logger.getLogger(Import.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
}
来电:
@PostConstruct
public void init() {
this.entityClass = User.class;
for (User user : (List<User>) Tools.getItems(User.class, "user.xml"))
em.persist(user);
}