简体   繁体   English

我们如何在一个xml文件中合并两个java对象?

[英]how can we merge two java objects in one xml file?

This is my program. 这是我的程序。 Is it correct way to merge two objects? 合并两个对象是否正确?

public class ObjectToXml {  
public static void main(String[] args) throws Exception{  
JAXBContext contextObj = JAXBContext.newInstance(Employee.class);  

Marshaller marshallerObj = contextObj.createMarshaller();  
marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  
try{
  Employee employees = new Employee();
  employees.setEmployee(new ArrayList<Employee>());

  Employee emp1=new Employee(1,"Vimal Jaiswal",50000);  
  Employee emp2=new Employee(2,"Kamal",40000);

  employees.getEmployee().add(emp1);
  employees.getEmployee().add(emp2);
  marshallerObj.marshal(employees, new FileOutputStream("E:\\employee.xml"));  

  }
    catch(JAXBException e){
  System.out.println(e);
 }}}

Its giving output Like doulbe times: 其给定输出类似于doulbe次:

1,"Vimal Jaiswal",50000 
2,"Kamal",40000
1,"Vimal Jaiswal",50000 
2,"Kamal",40000

Giving a precise answer without the implementation and annotation of your Employee class is hard to do. 在没有Employee类的实现和注释的情况下给出准确的答案很难。

Therefore I wrote a small example which is highly related to yours. 因此,我写了一个与您的案例高度相关的小例子。 I hope this is helpful :) 我希望这是有帮助的 :)

Hint: The important part is writing the XML-Annotations. 提示:重要的部分是编写XML注释。

Main.class 主类

import java.io.FileOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class Main {
    public static void main(String[] args) throws Exception {
        JAXBContext contextObj = JAXBContext.newInstance(Employees.class);

        Marshaller marshallerObj = contextObj.createMarshaller();
        marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        try {
            Employees employees = new Employees();
            Employee emp1 = new Employee(1, "Vimal Jaiswal", 50000);
            Employee emp2 = new Employee(2, "Kamal", 40000);

            employees.getEmployees().add(emp1);
            employees.getEmployees().add(emp2);
            marshallerObj.marshal(employees, new FileOutputStream(
                    "W:\\employee.xml"));

        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

Employees.class 员工班

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Employees {
    private List<Employee> employees;

    public Employees() {
        employees = new ArrayList<Employee>();
    }

    @XmlElement
    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }
}

Employee.class 员工班

import javax.xml.bind.annotation.XmlElement;

public class Employee {
    private int id;
    private String name;
    private int salary;

    public Employee(int id, String name, int salary) {
        this.id = id;
        this.name = name;
        this.salary = salary;
    }

    @XmlElement
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @XmlElement
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlElement
    public int getSalary() {
        return salary;
    }

    public void setSalary(int salary) {
        this.salary = salary;
    }
}

The resulting XML: 生成的XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employees>
    <employees>
        <id>1</id>
        <name>Vimal Jaiswal</name>
        <salary>50000</salary>
    </employees>
    <employees>
        <id>2</id>
        <name>Kamal</name>
        <salary>40000</salary>
    </employees>
</employees>

You should use try-with-resources to close the FileStream object: 您应该使用try-with-resources关闭FileStream对象:

try(FileOutputStream os = new FileOutputStream("c:\\temp\\employee.xml")){
 ...

 marshallerObj.marshal(employees, os);  

}

Create a class with list of Employee objects 用Employee对象列表创建一个类

public class EmployeeList {

private List<Employee> list;

public EmployeeList(){
    list = new ArrayList<Employee>();
}

public void add(Employee e){
    list.add(e);
}

} }

Then serialize this 然后序列化

XStream xstream = new XStream();
xstream.alias("employee", Employee.class);
xstream.alias("employees", EmployeeList.class);
xstream.addImplicitCollection(EmployeeList.class, "list");

EmployeeList list = new EmployeeList();
list.add(new Employee(1,"Vimal Jaiswal",50000));
list.add(new Employee(2,"Kamal",40000));

String xml = xstream.toXML(list);

I hope this will help. 我希望这将有所帮助。

Please find the below code to solve your problem, 请找到以下代码来解决您的问题,

package com.mss.sample;

import javax.xml.bind.annotation.XmlElement;

/**
 * Employee Model class
 * 
 * @author Anilkumar Bathula
 */


public class Employee {

    // Fields
    int empId;
    String empName;
    int empSal;

    public Employee(int empId, String empName, int empSal) {
        super();
        this.empId = empId;
        this.empName = empName;
        this.empSal = empSal;
    }

    @XmlElement
    public int getEmpId() {
        return empId;
    }

    public void setEmpId(int empId) {
        this.empId = empId;
    }

    @XmlElement
    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    @XmlElement
    public int getEmpSal() {
        return empSal;
    }

    public void setEmpSal(int empSal) {
        this.empSal = empSal;
    }

}

Serialize class: 序列化类:

package com.mss.sample;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * Employees class
 * 
 * @author Anilkumar Bathula
 */

@XmlRootElement
public class Employees {
    private List<Employee> employees;

    public Employees() {
        employees = new ArrayList<Employee>();
    }

    @XmlElement
    public List<Employee> getEmployees() {
        return employees;
    }

    public void setEmployees(List<Employee> employees) {
        this.employees = employees;
    }
}

Mainclass of generating XML: 生成XML的主类:

package com.mss.sample;

import java.io.FileOutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import com.mss.sample.Employees;

/**
 * Marshal class
 * 
 * @author Anilkumar Bathula
 */

public class Marshal {

    public static void main(String[] args) throws Exception {

        JAXBContext contextObj = JAXBContext.newInstance(Employees.class);

        Marshaller marshallerObj = contextObj.createMarshaller();
        marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        try {

            Employees employees = new Employees();
            Employee emp1 = new Employee(1, "Vimal Jaiswal", 50000);
            Employee emp2 = new Employee(2, "Kamal", 40000);

            employees.getEmployees().add(emp1);
            employees.getEmployees().add(emp2);
            marshallerObj.marshal(employees, new FileOutputStream(
                    "D:\\employee.xml"));

        } catch (JAXBException e) {
            System.out.println(e);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM