我有一个由 JAXB 生成的 XML 文件。我期望做的是将“添加和更新”操作放在单个方法中。“添加”操作可用于附加新条目/元素,但无法正确更新现有项目。
如果我想将“Damien”的 id 值更改为“P005”,它将创建具有相同名称但不同 id 的新元素(Damien,P005)。之前的值仍然存在(Damien,P004)。
有什么建议吗?之前谢谢。
这里是XML文件
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<getPersonsData>
<person>
<name>Alice</name>
<id>P001</id>
</person>
<person>
<name>Bob</name>
<id>P002</id>
</person>
<person>
<name>Charlie</name>
<id>P003</id>
</person>
<person>
<name>Damien</name>
<id>P004</id>
</person>
</getPersonsData>
这里是Person的JAXB类
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"name",
"id"
})
@XmlRootElement(name = "person")
public class Person {
@XmlElement(required = true)
protected String name;
@XmlElement(required = true)
protected String id;
public String getName() {
return name;
}
public void setName(String value) {
this.name= value;
}
public String getId() {
return id;
}
public void setid(String value) {
this.id = value;
}
}
这里是GetPersonsData的JAXB类
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"person"
})
@XmlRootElement(name = "getPersonsData")
public class GetPersonsData {
@XmlElement(name="person", required = true)
protected List<Person> person;
public List<Person> getPersons() {
if (person == null) {
person = new ArrayList<Person>();
}
return this.person;
}
}
这里是RESTFul Web Service (JAX-RS)类
@Path("/rest")
@Consumes({ MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON })
@Produces(MediaType.APPLICATION_JSON)
public class PersonConfig{
/**
* POST Method to add/update name and id
*
*/
@POST
@Path("/saveperson")
@Consumes({ MediaType.APPLICATION_FORM_URLENCODED,
MediaType.APPLICATION_JSON })
@Produces(MediaType.APPLICATION_JSON)
public void savePerson(@FormParam("name") String name,
@FormParam("id") String id) {
try {
String tesfile = "root/data/person.xml";
File file = new File(tesfile);
JAXBContext jc = JAXBContext.newInstance(GetPersonsData.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
// unmarshalling xml file
GetPersonsData getPersonsData = (GetPersonsData) unmarshaller
.unmarshal(file);
// get list of persons
List<Person> listPerson = getPersonsData.getPersons();
Iterator<Person> iter = listPerson.iterator();
Person person = iter.next();
//if person not exist, append new entry (element) to xml
//this code works, but I failed to update the existing
//element.
//If I want to update the id or name that already exist
//it will create new person with same name but different id and
//vice versa.
if(!person.getName().equals(name)){
Person p = new Person();
p.setName(name);
p.setId(id);
listPerson.add(p);
}else if (!person.getId().equals(id){
person.setId(id);
}
// Marshalling Object to the xml file
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(getConfigInputType, file);
m.marshal(getConfigInputType, System.out);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
}
}