使用jaxb时使用xml绑定异常

时间:2014-07-14 08:36:09

标签: java xml jaxb

我创建了下面的摆动形式,它将采用一些参数 来自表单中的用户,当用户点击按钮generatenetworkmodel时,应生成一个xml,在那些xml中,输入值与用户在文本框中输入的值相同,如图所示

我希望在表单中的JTextPane中生成和编写的xml是..

现在我选择了jaxb的方法并开发了以下pojo ..

package abcd;

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


@XmlRootElement(name = "route")
public class Route {



    @XmlAttribute(name = "cna")
    protected String cna;
    public String getCna(){ return cna; }
    public void setCna( String value ){ cna = value; }



    @XmlAttribute(name = "typ")
    protected String typ;
    public String getTyp() {
        return typ;
    }
    public void setTyp(String typ) {
        this.typ = typ;
    }



    @XmlAttribute(name = "gelcatnumbersink")
    protected String gelcatnumbersink;
    public String getGelcatnumbersink() {
        return gelcatnumbersink;
    }
    public void setGelcatnumbersink(String gelcatnumbersink) {
        this.gelcatnumbersink = gelcatnumbersink;
    }



    @XmlAttribute(name = "gelcatnumbersource")
    protected String gelcatnumbersource;
    public String getGelcatnumbersource() {
        return gelcatnumbersource;
    }
    public void setGelcatnumbersource(String gelcatnumbersource) {
        this.gelcatnumbersource = gelcatnumbersource;
    }



    @XmlAttribute(name = "postfiltertype")
    protected String postfiltertype;
    public String getPostfiltertype() {
        return postfiltertype;
    }
    public void setPostfiltertype(String postfiltertype) {
        this.postfiltertype = postfiltertype;
    }



    @XmlAttribute(name = "postfilterfilename")
    protected String postfilterfilename;
    public String getPostfilterfilename() {
        return postfilterfilename;
    }
    public void setPostfilterfilename(String postfilterfilename) {
        this.postfilterfilename = postfilterfilename;
    }



    @XmlAttribute(name = "prefiltertype")
    protected String prefiltertype;
    public String getPrefiltertype() {
        return prefiltertype;
    }
    public void setPrefiltertype(String prefiltertype) {
        this.prefiltertype = prefiltertype;
    }



    @XmlAttribute(name = "prefilterfilename")
    protected String prefilterfilename;
public String getPrefilterfilename() {
        return prefilterfilename;
    }
    public void setPrefilterfilename(String prefilterfilename) {
        this.prefilterfilename = prefilterfilename;
    }



    @XmlAttribute(name = "sk_ver")
    protected String sk_ver;
     public String getSk_ver() {
        return sk_ver;
    }
    public void setSk_ver(String sk_ver) {
        this.sk_ver = sk_ver;
    }



    @XmlAttribute(name = "sk_typ")
        protected String sk_typ;
     public String getSk_typ() {
        return sk_typ;
    }
    public void setSk_typ(String sk_typ) {
        this.sk_typ = sk_typ;
    }



    @XmlAttribute(name = "sk_cna")
    protected String sk_cna;
      public String getSk_cna() {
        return sk_cna;
    }
    public void setSk_cna(String sk_cna) {
        this.sk_cna = sk_cna;
    }



    @XmlAttribute(name = "ver")
    protected String ver;
    public String getVer() {
        return ver;
    }
    public void setVer(String ver) {
        this.ver = ver;
    }



}

以下是我的挥杆形式,按钮我必须生成xml ..

public class sdr {

    private JFrame frmArgonNetworkModel;
    private final JButton btnGenerateagonnetworkmodel = new JButton("GenerateAgonNetworkModel");
    private JTextField textField;
    private JTextField textField_1;
    private JTextField textField_2;
    private JTextField textField_3;
    private JTextField textField_4;
    private JTextField textField_5;
    private JTextField textField_6;
    private JTextField textField_7;
    private JTextField textField_8;
    private JTextField textField_9;
    private JTextField textField_10;
    private JTextField textField_11;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    sdr window = new sdr();
                    window.frmArgonNetworkModel.setVisible(true);

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

    /**
     * Create the application.
     */
    public sdr() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmArgonNetworkModel = new JFrame();
        frmArgonNetworkModel.getContentPane().setForeground(new Color(188, 143, 143));
        frmArgonNetworkModel.setForeground(new Color(245, 222, 179));
        frmArgonNetworkModel.setTitle("Argon Network Model");
        frmArgonNetworkModel.getContentPane().setBackground(UIManager.getColor("ToolBar.light"));
        frmArgonNetworkModel.getContentPane().setLayout(null);
        btnGenerateagonnetworkmodel.setBackground(new Color(224, 255, 255));
        btnGenerateagonnetworkmodel.setBounds(223, 329, 196, 29);

        frmArgonNetworkModel.getContentPane().add(btnGenerateagonnetworkmodel);

        JLabel lblNewLabel = new JLabel("Route CNA");
        lblNewLabel.setBounds(22, 11, 71, 14);
        frmArgonNetworkModel.getContentPane().add(lblNewLabel);

        JLabel lblTyp = new JLabel("typ");
        lblTyp.setBounds(22, 36, 71, 14);
        frmArgonNetworkModel.getContentPane().add(lblTyp);

        JLabel lblSkcna = new JLabel("sk_cna");
        lblSkcna.setBounds(22, 67, 71, 14);
        frmArgonNetworkModel.getContentPane().add(lblSkcna);

        JLabel lblSktyp = new JLabel("sk_typ");
        lblSktyp.setBounds(22, 92, 71, 14);
        frmArgonNetworkModel.getContentPane().add(lblSktyp);

        JLabel lblSkver = new JLabel("sk_ver");
        lblSkver.setBounds(22, 117, 71, 14);
        frmArgonNetworkModel.getContentPane().add(lblSkver);

        JLabel lblPrefilterfilename = new JLabel("prefilterfilename");
        lblPrefilterfilename.setBounds(22, 142, 105, 14);
        frmArgonNetworkModel.getContentPane().add(lblPrefilterfilename);

        textField = new JTextField();
        textField.setBounds(122, 8, 377, 20);
        frmArgonNetworkModel.getContentPane().add(textField);
        textField.setColumns(10);

        textField_1 = new JTextField();
        textField_1.setBounds(122, 33, 377, 20);
        frmArgonNetworkModel.getContentPane().add(textField_1);
        textField_1.setColumns(10);

        textField_2 = new JTextField();
        textField_2.setBounds(122, 64, 377, 20);
        frmArgonNetworkModel.getContentPane().add(textField_2);
        textField_2.setColumns(10);

        textField_3 = new JTextField();
        textField_3.setBounds(122, 89, 377, 20);
        frmArgonNetworkModel.getContentPane().add(textField_3);
        textField_3.setColumns(10);

        textField_4 = new JTextField();
        textField_4.setBounds(122, 114, 377, 20);
        frmArgonNetworkModel.getContentPane().add(textField_4);
        textField_4.setColumns(10);

        textField_5 = new JTextField();
        textField_5.setBounds(122, 139, 377, 20);
        frmArgonNetworkModel.getContentPane().add(textField_5);
        textField_5.setColumns(10);

        JLabel lblNewLabel_1 = new JLabel("prefiltertype");
        lblNewLabel_1.setBounds(22, 167, 88, 14);
        frmArgonNetworkModel.getContentPane().add(lblNewLabel_1);

        textField_6 = new JTextField();
        textField_6.setBounds(122, 164, 377, 20);
        frmArgonNetworkModel.getContentPane().add(textField_6);
        textField_6.setColumns(10);

        JLabel lblNewLabel_2 = new JLabel("postfilterfilename");
        lblNewLabel_2.setBounds(22, 192, 88, 14);
        frmArgonNetworkModel.getContentPane().add(lblNewLabel_2);

        textField_7 = new JTextField();
        textField_7.setBounds(122, 189, 377, 20);
        frmArgonNetworkModel.getContentPane().add(textField_7);
        textField_7.setColumns(10);

        JLabel lblNewLabel_3 = new JLabel("postfiltertype");
        lblNewLabel_3.setBounds(22, 217, 86, 14);
        frmArgonNetworkModel.getContentPane().add(lblNewLabel_3);

        JLabel lblNewLabel_4 = new JLabel("xfm");
        lblNewLabel_4.setBounds(22, 240, 46, 14);
        frmArgonNetworkModel.getContentPane().add(lblNewLabel_4);

        textField_8 = new JTextField();
        textField_8.setBounds(122, 214, 377, 20);
        frmArgonNetworkModel.getContentPane().add(textField_8);
        textField_8.setColumns(10);

        textField_9 = new JTextField();
        textField_9.setBounds(122, 237, 377, 20);
        frmArgonNetworkModel.getContentPane().add(textField_9);
        textField_9.setColumns(10);

        JLabel lblNewLabel_5 = new JLabel("gelcatnumbersource");
        lblNewLabel_5.setBounds(22, 265, 116, 14);
        frmArgonNetworkModel.getContentPane().add(lblNewLabel_5);

        JLabel lblNewLabel_6 = new JLabel("gelcatnumbersink");
        lblNewLabel_6.setBounds(22, 290, 88, 14);
        frmArgonNetworkModel.getContentPane().add(lblNewLabel_6);

        textField_10 = new JTextField();
        textField_10.setBounds(122, 262, 377, 20);
        frmArgonNetworkModel.getContentPane().add(textField_10);
        textField_10.setColumns(10);

        textField_11 = new JTextField();
        textField_11.setBounds(120, 290, 379, 20);
        frmArgonNetworkModel.getContentPane().add(textField_11);
        textField_11.setColumns(10);

        JTextPane textPane = new JTextPane();
        textPane.setBounds(22, 369, 652, 186);
        frmArgonNetworkModel.getContentPane().add(textPane);
        frmArgonNetworkModel.setBounds(100, 100, 711, 613);
        frmArgonNetworkModel.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        btnGenerateagonnetworkmodel.setActionCommand("generate");
        btnGenerateagonnetworkmodel.addActionListener(new ButtonClickListener());
    }

    public class ButtonClickListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub

            if ("generate".equals(e.getActionCommand())) {
                // create the Route object
                // marshal to string
                // ...

                Route route = new Route();
                route.setCna( "cnavalue" );
                try 
                {
                JAXBContext jc = JAXBContext.newInstance( Route.class );
                Marshaller m = jc.createMarshaller();
                m.setProperty( "jaxb.fragment", true );
                StringWriter sw = new StringWriter();
                m.marshal( route, sw );
                System.out.println( sw.toString() );
                }catch(Exception ee)
                {ee.printStackTrace();
            }

        }

    }




}
}

现在现在我正在接受例外......

com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 12 counts of IllegalAnnotationExceptions
Class has two properties of the same name "cna"
    this problem is related to the following location:
        at public java.lang.String abcd.Route.getCna()
        at abcd.Route
    this problem is related to the following location:
        at protected java.lang.String abcd.Route.cna
        at abcd.Route
Class has two properties of the same name "gelcatnumbersink"
    this problem is related to the following location:
        at public java.lang.String abcd.Route.getGelcatnumbersink()
        at abcd.Route
    this problem is related to the following location:
        at protected java.lang.String abcd.Route.gelcatnumbersink
        at abcd.Route
Class has two properties of the same name "gelcatnumbersource"
    this problem is related to the following location:
        at public java.lang.String abcd.Route.getGelcatnumbersource()
        at abcd.Route
    this problem is related to the following location:
        at protected java.lang.String abcd.Route.gelcatnumbersource
        at abcd.Route
Class has two properties of the same name "postfilterfilename"
    this problem is related to the following location:
        at public java.lang.String abcd.Route.getPostfilterfilename()
        at abcd.Route
    this problem is related to the following location:
        at protected java.lang.String abcd.Route.postfilterfilename
        at abcd.Route
Class has two properties of the same name "postfiltertype"
    this problem is related to the following location:
        at public java.lang.String abcd.Route.getPostfiltertype()
        at abcd.Route
    this problem is related to the following location:
        at protected java.lang.String abcd.Route.postfiltertype
        at abcd.Route
Class has two properties of the same name "prefilterfilename"
    this problem is related to the following location:
        at public java.lang.String abcd.Route.getPrefilterfilename()
        at abcd.Route
    this problem is related to the following location:
        at protected java.lang.String abcd.Route.prefilterfilename
        at abcd.Route
Class has two properties of the same name "prefiltertype"
    this problem is related to the following location:
        at public java.lang.String abcd.Route.getPrefiltertype()
        at abcd.Route
    this problem is related to the following location:
        at protected java.lang.String abcd.Route.prefiltertype
        at abcd.Route
Class has two properties of the same name "sk_cna"
    this problem is related to the following location:
        at public java.lang.String abcd.Route.getSk_cna()
        at abcd.Route
    this problem is related to the following location:
        at protected java.lang.String abcd.Route.sk_cna
        at abcd.Route
Class has two properties of the same name "sk_typ"
    this problem is related to the following location:
        at public java.lang.String abcd.Route.getSk_typ()
        at abcd.Route
    this problem is related to the following location:
        at protected java.lang.String abcd.Route.sk_typ
        at abcd.Route
Class has two properties of the same name "sk_ver"
    this problem is related to the following location:
        at public java.lang.String abcd.Route.getSk_ver()
        at abcd.Route
    this problem is related to the following location:
        at protected java.lang.String abcd.Route.sk_ver
        at abcd.Route
Class has two properties of the same name "typ"
    this problem is related to the following location:
        at public java.lang.String abcd.Route.getTyp()
        at abcd.Route
    this problem is related to the following location:
        at protected java.lang.String abcd.Route.typ
        at abcd.Route
Class has two properties of the same name "ver"
    this problem is related to the following location:
        at public java.lang.String abcd.Route.getVer()
        at abcd.Route
    this problem is related to the following location:
        at protected java.lang.String abcd.Route.ver
        at abcd.Route

    at com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException$Builder.check(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.getTypeInfoSet(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl.<init>(Unknown Source)
    at com.sun.xml.internal.bind.v2.runtime.JAXBContextImpl$JAXBContextBuilder.build(Unknown Source)
    at com.sun.xml.internal.bind.v2.ContextFactory.createContext(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
    at javax.xml.bind.ContextFinder.newInstance(Unknown Source)
    at javax.xml.bind.ContextFinder.find(Unknown Source)
    at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
    at javax.xml.bind.JAXBContext.newInstance(Unknown Source)
    at abcd.sdr$ButtonClickListener.actionPerformed(sdr.java:209)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$000(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

1 个答案:

答案 0 :(得分:0)

默认情况下,JAXB将属性视为已映射。由于您已注释字段JAXB现在认为您有一个字段具有相同名称的属性映射。要解决此问题,您可以执行以下操作之一:

  • 注释属性(获取或设置方法)而不是字段。
  • 在字段上保留注释,并使用@XmlAccessorType(XmlAccessType.FIELD)注释班级。