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 サービス (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();
}
}