java - 将编码的多个对象写入一个 xml 文件

标签 java jaxb

我正在为我的实践创建简单的学生详细信息任务。

我有以下类(class)作为 StudentModel

@XmlRootElement(name = "Student")
@XmlType(name = "StudentDetails",propOrder = { "studentName",  "fatherName", "rollNumber", "studentAge",
        "studentGender", "studentDOB", "addressLine1", "addressLine2", "city",
        "state", "country", "zipcode" })
public class StudentModel {

    @XmlElement
    private String studentName;
    @XmlElement
    private String fatherName;
    @XmlAttribute
    private int rollNumber;
    @XmlElement
    private int studentAge;
    @XmlElement
    private String studentGender;
    @XmlElement
    private String studentDOB;
    @XmlElement
    private String addressLine1;
    @XmlElement
    private String addressLine2;
    @XmlElement
    private String city;
    @XmlElement
    private String state;
    @XmlElement
    private String country;
    @XmlElement
    private int zipcode;

    }

下面的代码我用来从java对象创建xml文件

if( xml.exists() ){
        try {

            fr4 = new FileReader(file);
            br4 = new BufferedReader(fr4);

            while( (readFile = br4.readLine()) != null ){   

            line = new Scanner(readFile).useDelimiter(",");

            StudentModel toXml = new StudentModel(line.next(),line.next(),  Integer.parseInt(line.next()),
                                Integer.parseInt(line.next()), line.next(), line.next(), line.next(), line.next(),
line.next(), line.next(), line.next(), Integer.parseInt(line.next()));

            JAXBContext context;
            context = JAXBContext.newInstance(StudentModel.class);
            Marshaller marshal = context.createMarshaller();
            marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
                                Boolean.TRUE);

            fw2 = new FileWriter(xml, true);
            marshal.marshal(toXml, fw2);
        }
        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
    }
    }else{
            System.out.println("File not created");
        }

我的问题是我无法将多个对象放入一个文件中,并且输出如下所示。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<StudentModel>
    <studentName>peter</studentName>
    <fatherName>john</fatherName>
    <rollNumber>12</rollNumber>
    <studentAge>24</studentAge>
    <studentGender>m</studentGender>
    <studentDOB>16-06-1991</studentDOB>
    <addressLine1>ty</addressLine1>
    <addressLine2>yt</addressLine2>
    <city>new york</city>
    <state>ny</state>
    <country>us</country>
    <zipcode>456123</zipcode>
</StudentModel>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<StudentModel rollNumber="11">
    <studentName>john</studentName>
    <fatherName>peter</fatherName>
    <studentAge>24</studentAge>
    <studentGender>M</studentGender>
    <studentDOB>02-10-1991</studentDOB>
    <addressLine1>line1</addressLine1>
    <addressLine2>line2</addressLine2>
    <city>washington</city>
    <state>dc</state>
    <country>us</country>
    <zipcode>123456</zipcode>
</StudentModel>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>     

最佳答案

在顶部创建一个类并向其中添加一个 StudentModel 列表怎么样?所以新类将是根

@XmlRootElement(name = "Student")
public class Student{
  List<StudentModel> studentModels;
}

XML 会是这样的

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Student>
<StudentModel>
    <studentName>peter</studentName>
    <fatherName>john</fatherName>
    <rollNumber>12</rollNumber>
    <studentAge>24</studentAge>
    <studentGender>m</studentGender>
    <studentDOB>16-06-1991</studentDOB>
    <addressLine1>ty</addressLine1>
    <addressLine2>yt</addressLine2>
    <city>new york</city>
    <state>ny</state>
    <country>us</country>
    <zipcode>456123</zipcode>
</StudentModel>
<StudentModel rollNumber="11">
    <studentName>john</studentName>
    <fatherName>peter</fatherName>
    <studentAge>24</studentAge>
    <studentGender>M</studentGender>
    <studentDOB>02-10-1991</studentDOB>
    <addressLine1>line1</addressLine1>
    <addressLine2>line2</addressLine2>
    <city>washington</city>
    <state>dc</state>
    <country>us</country>
    <zipcode>123456</zipcode>
</StudentModel>
</Student>

关于java - 将编码的多个对象写入一个 xml 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34469554/

相关文章:

java - OrientDB 服务器部分忽略 ORIENTDB_HOME

eclipse - 未为 Eclipse 上的模式生成 JAXB 类

java - 无法初始化类 com.sun.xml.internal.ws.fault.SOAPFaultBuilder

java - 如果流包含集合则解码?

xml - Netbeans 模块中的 JAXB 绑定(bind)在哪里

java - 使用 Jersey 解析子节点

java - 为什么模式会忽略字符类中的空格

java - Gradle Eclipse 任务无法解析 MavenLocal 的依赖关系

java - 需要帮助编写 hashcode 和 equals 方法吗?

java - 具有字段名称和值的 XML 以响应无法从字段中检索的映射 Java 对象