我只是想循环一个具有相同 xml 元素但具有不同 ID 的 jaxb marshal,这与我下面的代码不同的是,它为每个 ID 解析新的 xml 文件并覆盖旧的。
data[][] 从矩阵中读取,data[i][0] 表示 ID 列表,我想将这些 id 设置为客户 ID。
Customer customer = new Customer();
File file = new File("C:/data.xml");
for (int i = 0; i < data.length; i++) {
customer.setId(data[i][0]);
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(customer, file);
jaxbMarshaller.marshal(customer, System.out);
}
上面代码的输出:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="1"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="2"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="3"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="4"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<customer id="5"/>
我想要一个 xml 文件中的所有 ID输出,有什么提示吗?