java - 导出为 XML,包括嵌入式类

标签 java xml xml-serialization

我有一个对象config,它有一些属性。我可以导出它,但是,它还有一个 ArrayList,它与嵌入类相关,当我导出到 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> 

最佳答案

Xstream从他们的描述中可以更好地处理这个问题:

不需要修改对象。序列化内部字段,包括 private 和 final。支持非公共(public)类和内部类。类不需要有默认构造函数。

关于java - 导出为 XML,包括嵌入式类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2593211/

相关文章:

java - 排序后获取未排序 double 组的索引

java - jsp页面转成Servlet类后,class文件在哪里找?

java - 使用 DOM 保存到 XML 时停止转义特殊字符

c# - XMLSerializer 不序列化类属性,只返回 bool - 'false' *指定成员的值

java - 具有 Kotlin 协程的函数是否可以等待完成并返回到 Java 类?

java - 将上下文从 Service 传递到 Asynctask 而不会泄漏

c# - 如何忽略将 xml 反序列化为对象时的 soap 内容?

c# - 使用 XML 类属性,如何用内部文本和属性表示 XML 标记?

java - 使用java处理空的xml标签

java - 如何让 Jackson 在序列化时不将字段输出到 XML