我需要创建具有以下结构的XML文件:
*@XmlPath("/Person/@ID")*
我已经在Stack Overflow上尝试了所有解决方案,使用*@XmlAttribute(name="ID")*
,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 节点之前创建其他节点。但属性应该在 Person 节点
Main.class中的方法:
public class Person {
private final IntegerProperty ID;
private final StringProperty name;
private final StringProperty surname;
private final StringProperty prevSurname;
private final StringProperty patronymic;
...
Person.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;
}
}
PersonListXMLWrapper.class:
SSL_CTX_set_options(ssl_list[i].ctx, SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | SSL_OP_CIPHER_SERVER_PREFERENCE| SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1);
答案 0 :(得分:0)
如果您可以使用其他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
答案 1 :(得分:0)
解决方案非常简单。我只需要在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>