Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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
JAXB编组Java以输出XML文件_Java_Xml_Jaxb_Marshalling - Fatal编程技术网

JAXB编组Java以输出XML文件

JAXB编组Java以输出XML文件,java,xml,jaxb,marshalling,Java,Xml,Jaxb,Marshalling,问题是如何生成XML文件输出而不是system.out package jaxbintroduction; import java.io.FileOutputStream; import java.io.OutputStream; public class Main { /** * @param args the command line arguments */ public static void main(String[] args) {

问题是如何生成XML文件输出而不是system.out

package jaxbintroduction;

import java.io.FileOutputStream;
import java.io.OutputStream;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        itemorder.Book quickXML = new itemorder.Book();

        quickXML.setAuthor("Sillyme");
        quickXML.setDescription("Dummie book");
        quickXML.setISBN(123456789);
        quickXML.setPrice((float)12.6);
        quickXML.setPublisher("Progress");
        quickXML.setTitle("Hello World JAVA");

        try {            
            javax.xml.bind.JAXBContext jaxbCtx = javax.xml.bind.JAXBContext.newInstance(quickXML.getClass().getPackage().getName());
            javax.xml.bind.Marshaller marshaller = jaxbCtx.createMarshaller();
            marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_ENCODING, "UTF-8"); //NOI18N
            marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshaller.marshal(quickXML, System.out);
            OutputStream os = new FileOutputStream( "nosferatu.xml" );
            marshaller.marshal( quickXML, os );

        } catch (javax.xml.bind.JAXBException ex) {
            // XXXTODO Handle exception
            java.util.logging.Logger.getLogger("global").log(java.util.logging.Level.SEVERE, null, ex); //NOI18N
        }
    }

}

您已经在编组到
nosferatu.xml
。只需删除或注释该行:

marshaller.marshal(quickXML, System.out);
如果不希望显示输出并关闭
OutputStream

os.close();

Marshaller#marshall(…)
方法将OutputStream或Writer作为参数。如果您查看过,肯定会在API中找到这一点。

如果您使用的是JAXB 2.1或更高版本,那么您可以直接封送到
java.io.File
对象:

 File file = new File( "nosferatu.xml" );
 marshaller.marshal( quickXML, file );
对应的Javadoc


这只是一个从java对象到xml文件的转换过程。 它与序列化非常相似,您必须确定序列化和封送。 这里我做了编组的样品。可以用类似的方法进行解组

带有jaxp注释的bean类:

package com.ofs.swinapps;

import javax.xml.bind.annotation.XmlRootElement;

    @XmlRootElement
    public class Employee {
    private String name;
    private String id;
    private String department;
    private int age;
    private int salary;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public int getAge() {
        return age;
}

    public void setAge(int age) {
        this.age = age;
    }

    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    }
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class Java2XMLExample {
    public static void main(String[] args) throws JAXBException {
 Employee employee = new Employee();
 employee.setName("Kowthal ganesh");
 employee.setAge(23);
 employee.setDepartment("Chola-ccms");
 employee.setId("947");
 employee.setSalary(8333);
 File file = new File("D:/build.xml");
 JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
 jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 jaxbMarshaller.marshal(employee, file);
    }
}
编组:

package com.ofs.swinapps;

import javax.xml.bind.annotation.XmlRootElement;

    @XmlRootElement
    public class Employee {
    private String name;
    private String id;
    private String department;
    private int age;
    private int salary;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public int getAge() {
        return age;
}

    public void setAge(int age) {
        this.age = age;
    }

    public int getSalary() {
        return salary;
    }
    public void setSalary(int salary) {
        this.salary = salary;
    }
    }
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class Java2XMLExample {
    public static void main(String[] args) throws JAXBException {
 Employee employee = new Employee();
 employee.setName("Kowthal ganesh");
 employee.setAge(23);
 employee.setDepartment("Chola-ccms");
 employee.setId("947");
 employee.setSalary(8333);
 File file = new File("D:/build.xml");
 JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
 Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
 jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
 jaxbMarshaller.marshal(employee, file);
    }
}

你看过API了吗?@Reimeus我也面临着类似的问题。你能看看吗