[英]JAXB: Create attribute for inner element
我需要创建具有以下结构的XML文件:
<Persons>
<Person ID="1">
<Name></Name>
...
</Person>
<Person ID="2">
<Name></Name>
...
</Person>
...
</Persons>
我已经在Stack Overflow上尝试了所有解决方案,使用*@XmlPath("/Person/@ID")*
, *@XmlAttribute(name="ID")*
并没有成功。 我的代码在根节点中创建属性,或者在“ 人”节点之前创建其他节点。 但是属性应该在Person节点内
Main.class中的方法:
public void savePersonDataToFile(File file) {
try {
JAXBContext context = JAXBContext
.newInstance(PersonListXMLWrapper.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// Wrapping our person data.
PersonListXMLWrapper wrapper = new PersonListXMLWrapper();
wrapper.setPersons(personData);
// Marshalling and saving XML to the file.
m.marshal(wrapper, file);
// Save the file path to the registry.
setPersonFilePath(file);
} catch (Exception e) { // catches ANY exception
Alert alert = new Alert(AlertType.ERROR);
alert.setTitle("Error");
alert.setHeaderText("Cant save to file:\n" + file.getPath());
alert.setContentText("Ooops, there was an error!");
}
}
Person.class具有变量声明,更改和获取它们的方法以及构造函数:
public class Person {
private final IntegerProperty ID;
private final StringProperty name;
private final StringProperty surname;
private final StringProperty prevSurname;
private final StringProperty patronymic;
...
PersonListXMLWrapper.class:
@XmlRootElement(name = "Persons")
public class PersonListXMLWrapper {
private List<Person> persons;
@XmlElement(name = "Person")
public List<Person> getPersons() {
return persons;
}
public void setPersons(List<Person> persons) {
this.persons = persons;
}
@XmlPath("/Person/@IDAtt")
public String IDAtt ="123";
@XmlAttribute(name="ID")
public String getID() {
return IDAtt;
}
public void setID(String ID) {
this.IDAtt = ID;
}
}
如果可以使用另一个XML解析器,我建议使用Scilca XML Progression编写一个简单的XML文件(对其进行验证)。
import org.scilca.sxp.*; // The SXP package
Node rootNode = new XMLNode("Persons");
// First Persion
Node p1 = Node.constructNode("<Person Id=\"1\"?");
p1.add("Name").setValue("Jacob"); // Add a child element and set its value
p1.add("Habit").setValue("Eating");
rootNode.add(p1); // Add it to root node
// Second Person
Node p2 = Node.constructNode("<Person Id=\"2\">");
p1.add("Name").setValue("Jacks");
rootNode.add(p2);
Document XmlDocument = new Document(rootNode);
XmlWriter xw = XmlDocument.getWriter();
xw.saveXml("D:/file.txt"); // text file to see if the file is correct
解决方案非常简单。 我只需要向Person.class中的方法声明中添加*@XmlAttribute(name="ID")*
而不是PersonListXMLWrapper.class:
public class Person {
private final StringProperty ID;
private final StringProperty name;
private final StringProperty surname;
private final StringProperty prevSurname;
private final StringProperty patronymic;
...
@XmlAttribute(name="ID")
public String getID() {
return ID.get();
}
public void setID(String ID) {
this.ID.set(ID);
}
public StringProperty IDProperty() {
return ID;
}
所以我得到以下结构:
<Persons>
<Person ID="1">
<name>Name1</name>
<surname></surname>
<prevSurname></prevSurname>
<patronymic></patronymic>
...
</Person>
<Person ID="2">
<name>Name2</name>
<surname></surname>
<prevSurname></prevSurname>
<patronymic></patronymic>
...
</Person>
...
</Persons>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.