我有一个对象 config,它有一些属性。我可以导出这个,但是,它也有一个数组列表,它与嵌入式类相关,当我导出到XML时,我不能让它出现。任何指针都是有用的。
导出方法
public String exportXML(config conf, String path) {
String success = "";
try {
FileOutputStream fstream = new FileOutputStream(path);
try {
XMLEncoder ostream = new XMLEncoder(fstream);
try {
ostream.writeObject(conf);
ostream.flush();
} finally {
ostream.close();
}
} finally {
fstream.close();
}
} catch (Exception ex) {
success = ex.getLocalizedMessage();
}
return success;
}
配置类*(删除一些细节以缩小规模)*
public class config {
protected String author = "";
protected String website = "";
private ArrayList questions = new ArrayList();
public config(){
}
public void addQuestion(String name) {
questions.add(new question(questions.size(), name));
}
public void removeQuestion(int id) {
questions.remove(id);
for (int c = 0; c <= questions.size(); c++) {
question q = (question) (questions.get(id));
q.setId(c);
}
questions.trimToSize();
}
public config.question getQuestion(int id){
return (question)questions.get(id);
}
/**
* There can be multiple questions per config.
* Questions store all the information regarding what questions are
* asked of the user, including images, descriptions, and answers.
*/
public class question {
protected int id;
protected String title;
protected ArrayList answers;
public question(int id, String title) {
this.id = id;
this.title = title;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void addAnswer(String name) {
answers.add(new answer(answers.size(), name));
}
public void removeAnswer(int id) {
answers.remove(id);
for (int c = 0; c <= answers.size(); c++) {
answer a = (answer) (answers.get(id));
a.setId(c);
}
answers.trimToSize();
}
public config.question.answer getAnswer(int id){
return (answer)answers.get(id);
}
public class answer {
protected int id;
protected String title;
public answer(int id, String title) {
this.id = id;
this.title = title;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
}
}
生成的XML文件
<?xml version="1.0" encoding="UTF-8"?>
<java version="1.6.0_18" class="java.beans.XMLDecoder">
<object class="libConfig.config">
<void property="appName">
<string>xxx</string>
</void>
<void property="author">
<string>Andy</string>
</void>
<void property="website">
<string>www.example.com/dsx.xml</string>
</void>
</object>
</java>
3条答案
按热度按时间57hvy0tb1#
Xstream处理这一点要好得多,从他们的描述来看:
cclgggtu2#
Jackson是一个有能力的库,最初是JSON库/解析器,但现在支持XML以及Smile、(Java) Properties、YAML、CSV、CBOR、TOML、Avro、Protobuf等。
对于XML,可以像这样使用它:
一些有用的链接:
How to Marshal and Unmarshal XML with Jackson
Libraries - IntelliJ IDEA Documentation
eulz3vhy3#
XMLEncoder适用于公共getter和setter:http://www.exampledepot.com/egs/java.beans/WriteXml.html
我想知道,为什么其他属性(如作者)在文件中(?)尝试添加公共
getQuestions()
和setQuestions()
方法。