从jaxb生成正确的xml

时间:2014-07-14 14:11:11

标签: java xml jaxb

我通过从swing form中的用户获取输入来生成xml,现在我希望将xml参数值设置为用户输入的内容,例如user为以下参数输入以下值...

route cna="trk"
 typ="e_pwap"
 ver="96"
 sk_cna="ltp6"
 sk_typ="55"
 sk_ver="26"
 prefilterfilename="sjj"
 prefiltertype="true"
 postfilterfilename=""
 postfiltertype=""
 xfm=""
 gelcatnumbersource="16"
 gelcatnumbersink="40" 

所以应该生成最终的xml ...

  <route cna="trk" typ="e_pwap" ver="96" sk_cna="ltp6" sk_typ="55" sk_ver="26" prefilterfilename="sjj" prefiltertype="true" postfilterfilename="" postfiltertype="" xfm="" gelcatnumbersource="16" gelcatnumbersink="40" /> 

现在即使用户输入值也没有发生,现在它正在打印为...

  <route cna="cnavalue" typ="typ" ver="ver" sk_cna="sk_cna" sk_typ="sk_typ" sk_ver="sk_ver" prefilterfilename="prefilterfilename" prefiltertype="prefiltertype" postfilterfilename="postfilterfilename" postfiltertype="postfiltertype" gelcatnumbersource="gelcatnumbersource" gelcatnumbersink="gelcatnumbersink"/>

请告知如何纠正此问题。下面是我的pojo ..

package abcd;

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


@XmlRootElement(name = "route")
@XmlAccessorType(XmlAccessType.FIELD)
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 = "ver")
    protected String ver;
    public String getVer() {
        return ver;
    }
    public void setVer(String ver) {
        this.ver = ver;
    }


    @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 = "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_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 = "prefilterfilename")
    protected String prefilterfilename;
public String getPrefilterfilename() {
        return prefilterfilename;
    }
    public void setPrefilterfilename(String prefilterfilename) {
        this.prefilterfilename = prefilterfilename;
    }


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



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


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




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



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



}

以下是我的表格类..

package abcd;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import javax.swing.JButton;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.StringWriter;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JTextArea;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

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;
    private JLabel lblVer;
    private JTextField textField_12;

    /**
     * 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(226, 358, 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, 83, 71, 14);
        frmArgonNetworkModel.getContentPane().add(lblSkcna);

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

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

        JLabel lblPrefilterfilename = new JLabel("prefilterfilename");
        lblPrefilterfilename.setBounds(22, 158, 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, 80, 377, 20);
        frmArgonNetworkModel.getContentPane().add(textField_2);
        textField_2.setColumns(10);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        btnGenerateagonnetworkmodel.setActionCommand("generate");

        lblVer = new JLabel("Ver");
        lblVer.setBounds(32, 61, 46, 14);
        frmArgonNetworkModel.getContentPane().add(lblVer);

        textField_12 = new JTextField();
        textField_12.setBounds(122, 58, 377, 20);
        frmArgonNetworkModel.getContentPane().add(textField_12);
        textField_12.setColumns(10);
        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" );
                route.setTyp("typ");
                route.setVer("ver");
                route.setSk_cna("sk_cna");
                route.setSk_typ("sk_typ");
                route.setSk_ver("sk_ver");
                route.setPrefilterfilename("prefilterfilename");
                route.setPrefiltertype("prefiltertype");
                route.setPostfilterfilename("postfilterfilename");
                route.setPostfiltertype("postfiltertype");
                route.setGelcatnumbersource("gelcatnumbersource");
                route.setGelcatnumbersink("gelcatnumbersink");


                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();
            }

        }

    }




}
}

1 个答案:

答案 0 :(得分:1)

好吧,将字段的值设置为&#34; cnavalue&#34;确实会导致XML包含属性cnavalue =&#34; cnavalue&#34;。

            Route route = new Route();
            route.setCna( "cnavalue" );

使用类似

的内容
            route.setCna( textField_1.getText() );