JAXB解组问题
问题描述:
请,可能是什么毛病我code..below是我的菜单类..JAXB解组问题
package commuinity;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Menu {
String menu;
String menuitem;
String name;
String action;
public String getMenu() {
return menu;
}
@XmlElement
public void setMenu(String menu) {
this.menu = menu;
}
public String getMenuitem() {
return menuitem;
}
@XmlElement
public void setMenuitem(String menuitem) {
this.menuitem = menuitem;
}
public String getName() {
return name;
}
@XmlAttribute
public void setName(String name) {
this.name = name;
}
public String getAction() {
return action;
}
@XmlAttribute
public void setAction(String action) {
this.action = action;
}
}
,这里是我的JAXB实用类
import java.io.File;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class JAXBExcer {
public JAXBExcer(){
try {
File file = new File("sr/resources/file.xml");
file.createNewFile();
JAXBContext jaxbContext = JAXBContext.newInstance(Menu.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Menu menuApp= (Menu)jaxbUnmarshaller.unmarshal(file);
System.out.println(menuApp);
}
catch(IOException e){
System.out.println("File is not created!");
}
catch (JAXBException e) {
System.out.println("JaxB having issues!");
}
}
}
的问题是它不会创建该文件。它进入第一个口头短语“文件没有创建”我试图通过在我的文件系统中手动创建它,但然后跳转到下一个口号“JAXB有问题”任何人都可以指出我的原因吗?我只是在试验JAXB。我正在关注此链接上的教程:http://www.mkyong.com/java/jaxb-hello-world-example/
谢谢
您可以看看第4步:将XML转换为对象:http://www.mkyong.com/java/jaxb-hello-world-example/ 客户类与我的Menu类。可能还有其他的博客作者,但到目前为止,这是我在写我的时候看到的。 – helpdesk 2013-03-15 10:33:20
@henryjoseph - 该教程的第4步是从第3步创建的文件中读取的。 – 2013-03-15 10:35:35
Hello @ Blaise,你是否想说传递给Customer customer =(Customer)jaxbUnmarshaller.unmarshal(file)行的文件是从第3步文件?如果你的答案是肯定的,那么这对任何人来说都意味着使用JAXB,他必须在解组之前首先进行编组?这对我来说并没有什么说服力,因为我只是想解开从外部源下载的已经存在的XML文件并将其转换为Java对象。我不能创建一个空文件并将其传递给unmarshall()方法吗? – helpdesk 2013-03-15 10:39:48