如何使用Jaxb从XML获取属性?

时间:2017-01-14 19:16:55

标签: java xml jaxb

我已经尝试了一些方法,但似乎是我正在做的事情。 我创建了一个简单的Java类:

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

@XmlRootElement(name = "TRANSACTION")
@XmlAccessorType(XmlAccessType.FIELD)
public class CanonicalPropertyJaxb {

    @XmlAttribute
    protected String edbt;

    public String getEdbt() {
        return edbt;
    }

    public void setEdbt(String edbt) {
        this.edbt = edbt;
    }



}

这就是我收到的XML。我需要这个XML来生成Java对象和Java对象,以便在需要时生成相同的XML。所以我开始尝试从TRANSACTION标签获取EDBT属性,但它总是返回null。

<TRANSACTION ETYPE='JDEMCU' EDUS='IPAAS_ADM' EDBT='16919' EDTN='0' EDLN='2.000' EKEY='IPAAS_ADM-16919-0-2.000'>     <TABLE_1 NAME='F0006Z1' TYPE='1'>       <FORMAT_TABLE_F0006Z1 SEQ="1">         <COLUMN_EDUS>IPAAS_ADM</COLUMN_EDUS>         <COLUMN_EDBT>16919</COLUMN_EDBT>         <COLUMN_EDTN>0</COLUMN_EDTN>         <COLUMN_EDLN>2.000</COLUMN_EDLN>         <COLUMN_EDCT></COLUMN_EDCT>         <COLUMN_TYTN>JDEMCU</COLUMN_TYTN>         <COLUMN_EDFT></COLUMN_EDFT>         <COLUMN_EDDT />         <COLUMN_DRIN>2</COLUMN_DRIN>         <COLUMN_EDDL>0</COLUMN_EDDL>         <COLUMN_EDSP></COLUMN_EDSP>         <COLUMN_PNID></COLUMN_PNID>         <COLUMN_TNAC>02</COLUMN_TNAC>         <COLUMN_MCU>200</COLUMN_MCU>         <COLUMN_STYL>CB</COLUMN_STYL>         <COLUMN_DC>FINANCEREPORTTESTBSBU</COLUMN_DC>         <COLUMN_LDM>3</COLUMN_LDM>         <COLUMN_CO>00200</COLUMN_CO>         <COLUMN_AN8>0</COLUMN_AN8>         <COLUMN_AN8O>0</COLUMN_AN8O>         <COLUMN_CNTY></COLUMN_CNTY>         <COLUMN_ADDS></COLUMN_ADDS>         <COLUMN_FMOD></COLUMN_FMOD>         <COLUMN_DL01>Finance Report Test BS BU</COLUMN_DL01>         <COLUMN_DL02></COLUMN_DL02>         <COLUMN_DL03></COLUMN_DL03>         <COLUMN_DL04></COLUMN_DL04>         <COLUMN_RP01>DOM</COLUMN_RP01>         <COLUMN_RP02>CAN</COLUMN_RP02>         <COLUMN_RP03>NAA</COLUMN_RP03>         <COLUMN_RP04>NAA</COLUMN_RP04>         <COLUMN_RP05>NAA</COLUMN_RP05>         <COLUMN_RP06>ZZ</COLUMN_RP06>         <COLUMN_RP07>NAA</COLUMN_RP07>         <COLUMN_RP08>NAA</COLUMN_RP08>         <COLUMN_RP09>NAA</COLUMN_RP09>         <COLUMN_RP10>NAA</COLUMN_RP10>         <COLUMN_RP11>NAA</COLUMN_RP11>         <COLUMN_RP12>NAA</COLUMN_RP12>         <COLUMN_RP13>NAA</COLUMN_RP13>         <COLUMN_RP14>NAA</COLUMN_RP14>         <COLUMN_RP15>NAA</COLUMN_RP15>         <COLUMN_RP16>NAA</COLUMN_RP16>         <COLUMN_RP17>NAA</COLUMN_RP17>         <COLUMN_RP18>NAA</COLUMN_RP18>         <COLUMN_RP19>NAA</COLUMN_RP19>         <COLUMN_RP20>NAA</COLUMN_RP20>         <COLUMN_RP21>NAA</COLUMN_RP21>         <COLUMN_RP22>NAA</COLUMN_RP22>         <COLUMN_RP23>NAA</COLUMN_RP23>         <COLUMN_RP24></COLUMN_RP24>         <COLUMN_RP25></COLUMN_RP25>         <COLUMN_RP26></COLUMN_RP26>         <COLUMN_RP27></COLUMN_RP27>         <COLUMN_RP28></COLUMN_RP28>         <COLUMN_RP29></COLUMN_RP29>         <COLUMN_RP30></COLUMN_RP30>         <COLUMN_TA></COLUMN_TA>         <COLUMN_TXJS>0</COLUMN_TXJS>         <COLUMN_TXA1></COLUMN_TXA1>         <COLUMN_EXR1></COLUMN_EXR1>         <COLUMN_TC01></COLUMN_TC01>         <COLUMN_TC02></COLUMN_TC02>         <COLUMN_TC03></COLUMN_TC03>         <COLUMN_TC04></COLUMN_TC04>         <COLUMN_TC05></COLUMN_TC05>         <COLUMN_TC06></COLUMN_TC06>         <COLUMN_TC07></COLUMN_TC07>         <COLUMN_TC08></COLUMN_TC08>         <COLUMN_TC09></COLUMN_TC09>         <COLUMN_TC10></COLUMN_TC10>         <COLUMN_ND01></COLUMN_ND01>         <COLUMN_ND02></COLUMN_ND02>         <COLUMN_ND03></COLUMN_ND03>         <COLUMN_ND04></COLUMN_ND04>         <COLUMN_ND05></COLUMN_ND05>         <COLUMN_ND06></COLUMN_ND06>         <COLUMN_ND07></COLUMN_ND07>         <COLUMN_ND08></COLUMN_ND08>         <COLUMN_ND09></COLUMN_ND09>         <COLUMN_ND10></COLUMN_ND10>         <COLUMN_CC01>N</COLUMN_CC01>         <COLUMN_CC02>N</COLUMN_CC02>         <COLUMN_CC03>N</COLUMN_CC03>         <COLUMN_CC04>N</COLUMN_CC04>         <COLUMN_CC05>N</COLUMN_CC05>         <COLUMN_CC06>N</COLUMN_CC06>         <COLUMN_CC07>N</COLUMN_CC07>         <COLUMN_CC08>N</COLUMN_CC08>         <COLUMN_CC09>N</COLUMN_CC09>         <COLUMN_CC10>N</COLUMN_CC10>         <COLUMN_PECC></COLUMN_PECC>         <COLUMN_ALS>6</COLUMN_ALS>         <COLUMN_ISS>T</COLUMN_ISS>         <COLUMN_GLBA></COLUMN_GLBA>         <COLUMN_ALCL>00</COLUMN_ALCL>         <COLUMN_LMTH></COLUMN_LMTH>         <COLUMN_LF>1.0000</COLUMN_LF>         <COLUMN_OBJ1></COLUMN_OBJ1>         <COLUMN_OBJ2></COLUMN_OBJ2>         <COLUMN_OBJ3></COLUMN_OBJ3>         <COLUMN_SUB1></COLUMN_SUB1>         <COLUMN_TOU>.00</COLUMN_TOU>         <COLUMN_SBLI></COLUMN_SBLI>         <COLUMN_ANPA>0</COLUMN_ANPA>         <COLUMN_CT>FF</COLUMN_CT>         <COLUMN_CERT>N</COLUMN_CERT>         <COLUMN_MCUS></COLUMN_MCUS>         <COLUMN_BTYP></COLUMN_BTYP>         <COLUMN_PC>.00</COLUMN_PC>         <COLUMN_PCA>.00</COLUMN_PCA>         <COLUMN_PCC>.00</COLUMN_PCC>         <COLUMN_INTA></COLUMN_INTA>         <COLUMN_INTL></COLUMN_INTL>         <COLUMN_D1J />         <COLUMN_D2J />         <COLUMN_D3J />         <COLUMN_D4J />         <COLUMN_D5J />         <COLUMN_D6J />         <COLUMN_FPDJ />         <COLUMN_CAC>.00</COLUMN_CAC>         <COLUMN_PAC>.00</COLUMN_PAC>         <COLUMN_EEO>N</COLUMN_EEO>         <COLUMN_ERC></COLUMN_ERC>         <COLUMN_USER>IPAAS_ADM</COLUMN_USER>         <COLUMN_PID>ER0006Z1E</COLUMN_PID>         <COLUMN_UPMJ>2016/12/15</COLUMN_UPMJ>         <COLUMN_JOBN>DJDELOG02</COLUMN_JOBN>         <COLUMN_UPMT>144810</COLUMN_UPMT>         <COLUMN_URCD></COLUMN_URCD>         <COLUMN_URAT>.00</COLUMN_URAT>         <COLUMN_URDT />         <COLUMN_URAB>0</COLUMN_URAB>         <COLUMN_URRF></COLUMN_URRF>         <COLUMN_TORG></COLUMN_TORG>         <COLUMN_RP31></COLUMN_RP31>         <COLUMN_RP32></COLUMN_RP32>         <COLUMN_RP33></COLUMN_RP33>         <COLUMN_RP34></COLUMN_RP34>         <COLUMN_RP35></COLUMN_RP35>         <COLUMN_RP36></COLUMN_RP36>         <COLUMN_RP37></COLUMN_RP37>         <COLUMN_RP38></COLUMN_RP38>         <COLUMN_RP39></COLUMN_RP39>         <COLUMN_RP40></COLUMN_RP40>         <COLUMN_RP41></COLUMN_RP41>         <COLUMN_RP42></COLUMN_RP42>         <COLUMN_RP43></COLUMN_RP43>         <COLUMN_RP44></COLUMN_RP44>         <COLUMN_RP45></COLUMN_RP45>         <COLUMN_RP46></COLUMN_RP46>         <COLUMN_RP47></COLUMN_RP47>         <COLUMN_RP48></COLUMN_RP48>         <COLUMN_RP49></COLUMN_RP49>         <COLUMN_RP50></COLUMN_RP50>         <COLUMN_AN8GCA1>0</COLUMN_AN8GCA1>         <COLUMN_AN8GCA2>0</COLUMN_AN8GCA2>         <COLUMN_AN8GCA3>0</COLUMN_AN8GCA3>         <COLUMN_AN8GCA4>0</COLUMN_AN8GCA4>         <COLUMN_AN8GCA5>0</COLUMN_AN8GCA5>       </FORMAT_TABLE_F0006Z1>     </TABLE_1>   </TRANSACTION>

获取此信息的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

JAXB区分大小写。因此,要么将字段更改为EDBT,要么将大写名称添加到@XmlAttribute(name="EDBT")

我更喜欢后者,因为它符合java风格指南。