如何使用JAXB获取xml属性和值

时间:2022-02-28 21:50:04

I am new in Jaxb i have one xml file which contain many attribute so i want the attribute with value

我是Jaxb的新手,我有一个xml文件,其中包含许多属性,所以我希望该属性具有值

My XMl

     <message_mapping>    
<message Rtype="DIAGNOSTIC" direction="2" name="Diagnostic" mode="">
            <field tag="USERNAME"       source="I" tranData="username"  required="false" dataType="string" defaultValue="" />
            <field tag="PASSWORD"       source="I" tranData="password"  required="true"  dataType="string" defaultValue="" />
            <field tag="LOCALDATETIME"  source="E" tranData="trxDateTime" required="true"     dataType="string" defaultValue=""/>
</message>
</message_mapping>

1 个解决方案

#1


0  

I suppose you have your class MessageMapping.java which has in turn a list (or one? dunno) of message of type Message.java. Message.java in turn will be structured with a list of Field of type Field.java. The classes will be as follow:

我想你有你的MessageMapping.java类,它依次是Message.java类型的消息列表(或者一个?dunno)。反过来,Message.java将使用Field.java类型的Field列表进行结构化。课程如下:

    @XmlAccessorType(XmlAccessType.FIELD)
    public class Field {

        @XmlAttribute
        private String tag;

        @XmlAtrribute
        private String source;

        @XmlAtrribute
        private String tranData;

        @XmlAtrribute
        private String dataType;

        @XmlAtrribute
        private String defaultValue;

        /*
        GETTER AND SETTER HERE
        */

    }

And Message.java like this:

和Message.java这样:

        @XmlAccessorType(XmlAccessType.FIELD)
        public class Message {
            private List<Field> fields;
            private String Rtype;
            private String direction;
            private String name;
            private String mode;

            /*
            GETTER AND SETTER HERE
            */
        }

And ultimately message_mapping class will need to be built as you see fit to accomodate a list of message or a single one, dunno what are your specification. Hope it helps.

最终需要根据您的需要构建message_mapping类以容纳消息列表或单个消息列表,不知道您的规范是什么。希望能帮助到你。

#1


0  

I suppose you have your class MessageMapping.java which has in turn a list (or one? dunno) of message of type Message.java. Message.java in turn will be structured with a list of Field of type Field.java. The classes will be as follow:

我想你有你的MessageMapping.java类,它依次是Message.java类型的消息列表(或者一个?dunno)。反过来,Message.java将使用Field.java类型的Field列表进行结构化。课程如下:

    @XmlAccessorType(XmlAccessType.FIELD)
    public class Field {

        @XmlAttribute
        private String tag;

        @XmlAtrribute
        private String source;

        @XmlAtrribute
        private String tranData;

        @XmlAtrribute
        private String dataType;

        @XmlAtrribute
        private String defaultValue;

        /*
        GETTER AND SETTER HERE
        */

    }

And Message.java like this:

和Message.java这样:

        @XmlAccessorType(XmlAccessType.FIELD)
        public class Message {
            private List<Field> fields;
            private String Rtype;
            private String direction;
            private String name;
            private String mode;

            /*
            GETTER AND SETTER HERE
            */
        }

And ultimately message_mapping class will need to be built as you see fit to accomodate a list of message or a single one, dunno what are your specification. Hope it helps.

最终需要根据您的需要构建message_mapping类以容纳消息列表或单个消息列表,不知道您的规范是什么。希望能帮助到你。