1

请,我的代码可能有什么问题..下面是我的菜单类..

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

1 回答 1

1

解组是将 XML 转换为对象的过程。JAXB (JSR-222)实现要求文件包含 XML 内容。编组是从对象创建 XML 文件的过程,这可能是您正在寻找的。

于 2013-03-15T10:30:13.240 回答