Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将两个java对象合并到一个xml文件中?_Java_Xml - Fatal编程技术网

如何将两个java对象合并到一个xml文件中?

如何将两个java对象合并到一个xml文件中?,java,xml,Java,Xml,这是我的节目。合并两个对象是否正确 public class ObjectToXml { public static void main(String[] args) throws Exception{ JAXBContext contextObj = JAXBContext.newInstance(Employee.class); Marshaller marshallerObj = contextObj.createMarshaller(); marshallerObj.s

这是我的节目。合并两个对象是否正确

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);
 }}}

您应该使用try with resources关闭
FileStream
对象:

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

 marshallerObj.marshal(employees, os);  

}

创建一个包含Employee对象列表的类

public class EmployeeList {

private List<Employee> list;

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

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

我希望这会有所帮助。

如果没有Employee类的实现和注释,很难给出准确的答案

因此,我写了一个小例子,这是高度相关的你。我希望这有帮助:)

提示:重要的部分是编写XML注释

主类

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();
        }
    }
}
雇员阶级

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;
    }
}
生成的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>

1.
维马尔·贾斯瓦尔
50000
2.
卡莫尔
40000

请查找以下代码以解决您的问题

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;
    }

}
序列化类:

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;
    }
}
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);
        }
    }
}

如果您能上传xml输出,您将给出CSV文件类型dataXStream getting错误。我没有得到xstream-1.4.7。罐子
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;
    }

}
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;
    }
}
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);
        }
    }
}