[英]JAXB Java Marshalling error
我試圖用JAXB封送一個簡單的Java類,但遇到了錯誤。 我正在尋找解決以下錯誤的建議,這些是錯誤:
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
當我嘗試為JAXB封送處理運行類時,將出現錯誤。 它是從教程網站( http://www.mkyong.com/java/jaxb-hello-world-example/ )下載的,因此應該是正確的。
這是元帥課:
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();
}
}
}
封送處理類引用Customer類。 這是客戶類別:
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;
}
}
該錯誤似乎與XML無關-您根本無權訪問您要打開的文件。
嘗試刪除“ C:\\\\”,以使該文件位於工作目錄中,而不是C驅動器的頂部。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.