简体   繁体   English

JAXB Java编组错误

[英]JAXB Java Marshalling error

I am trying to marshal a simple Java class with JAXB and I am running into errors. 我试图用JAXB封送一个简单的Java类,但遇到了错误。 I am looking for advice on fixing the errors bellow These are the errors: 我正在寻找解决以下错误的建议,这些是错误:

javax.xml.bind.JAXBException
 - with linked exception:
[java.io.FileNotFoundException: C:\file.xml (Access is denied)]
    at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(Unknown Source)
    at JAXBExample.main(JAXBExample.java:23)
Caused by: java.io.FileNotFoundException: C:\file.xml (Access is denied)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(Unknown Source)
    at java.io.FileOutputStream.<init>(Unknown Source)
    ... 2 more

The errors come when I attempt to run the class for JAXB marshaling. 当我尝试为JAXB封送处理运行类时,将出现错误。 It was downloaded from a tutorial website ( http://www.mkyong.com/java/jaxb-hello-world-example/ ) so It should be correct. 它是从教程网站( http://www.mkyong.com/java/jaxb-hello-world-example/ )下载的,因此应该是正确的。

This is the Marshal class: 这是元帅课:

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

public class JAXBExample {
    public static void main(String[] args) {

      Customer customer = new Customer();
      customer.setId(100);
      customer.setName("mkyong");
      customer.setAge(29);

      try {

        File file = new File("C:\\file.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        jaxbMarshaller.marshal(customer, file);
        jaxbMarshaller.marshal(customer, System.out);

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

The Marshaling class references a Customer class. 封送处理类引用Customer类。 This is the customer class: 这是客户类别:

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

@XmlRootElement
public class Customer {

    String name;
    int age;
    int id;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public int getId() {
        return id;
    }

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

}

The error appears to have nothing to do with XML - you simply don't have access to the file that you are trying to open. 该错误似乎与XML无关-您根本无权访问您要打开的文件。

Try removing the "C:\\\\" so that the file will be in the working directory instead of the top of the C drive. 尝试删除“ C:\\\\”,以使该文件位于工作目录中,而不是C驱动器的顶部。

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

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