将带有其他节点的XML转换为Java对象

时间:2019-04-25 11:25:31

标签: java xml jaxb unmarshalling

我的XML如下。

<TRX_MESSAGE>
    <MESSAGE>
            <MESSAGE_ID>generate_qr_ack</MESSAGE_ID>
            <QR_CODE>7D3796E48</QR_CODE>
            <RESP_CODE>00</RESP_CODE>
            <RESP_DESC>success</RESP_DESC>
        </MESSAGE>
    </TRX_MESSAGE>

我想将此XML转换为Java类,但是我的模型Java类没有TRX_MESSAGEMESSAGE字段。

public class QR_Code_Resp
{
    @JsonProperty("MESSAGE_ID")
    private String MESSAGE_ID;

    @JsonProperty("QR_CODE")
    private String QR_CODE;

    @JsonProperty("RESP_CODE")
    private String RESP_CODE;

    @JsonProperty("RESP_DESC")
    private String RESP_DESC;

    //GETTERS AND SETTERS
    @XmlElement(name = "MESSAGE_ID")
    public String getMESSAGE_ID()
    {
        return MESSAGE_ID;
    }

    public void setMESSAGE_ID(String MESSAGE_ID)
    {
        this.MESSAGE_ID = MESSAGE_ID;
    }

    @XmlElement(name = "QR_CODE")
    public String getQR_CODE()
    {
        return QR_CODE;
    }

    public void setQR_CODE(String sQR_CODE)
    {
        this.QR_CODE = sQR_CODE;
    }

    @XmlElement(name = "RESP_CODE")
    public String getRESP_CODE()
    {
        return RESP_CODE;
    }

    public void setRESP_CODE(String RESP_CODE)
    {
        this.RESP_CODE = RESP_CODE;
    }

    @XmlElement(name = "RESP_DESC")
    public String getRESP_DESC()
    {
        return RESP_DESC;
    }

    public void setRESP_DESC(String RESP_DESC)
    {
        this.RESP_DESC = RESP_DESC;
    } 
}

我正尝试将XML转换为Java对象,请注意,第一行中的'test'字符串是我前面提到的XML输入。

    StringReader sr = new StringReader(test);
    JAXBContext jaxbContext = JAXBContext.newInstance(QR_Code_Resp.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    QR_Code_Resp response = (QR_Code_Resp) unmarshaller.unmarshal(sr);

我得到如下错误:错误是不言自明的,但是我想要的是一种解决方法,因此我不必更改我的QR_Code_Resp模型类。有办法吗?

Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"MESSAGE_ID"). Expected elements are (none)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:726)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:247)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:242)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:109)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1131)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:556)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:538)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:153)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:374)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:613)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3132)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:852)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:602)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:505)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:842)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:771)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:243)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:214)
    at sockettest.ClientTest.main(ClientTest.java:56)

先谢谢了。 :)

1 个答案:

答案 0 :(得分:1)

您的xml文件具有一个根元素。但QR_Code_Resp.java

中没有注释根元素

尝试以下pojo类

Message.java

@XmlRootElement(name="MESSAGE")
public class Message {

    private String messageId;
    private String qrCode;
    private String respCode;
    private String respDesc;

    @XmlElement(name="MESSAGE_ID")
    public String getMessageId() {
        return messageId;
    }
    public void setMessageId(String messageId) {
        this.messageId = messageId;
    }
    @XmlElement(name="QR_CODE")
    public String getQrCode() {
        return qrCode;
    }
    public void setQrCode(String qrCode) {
        this.qrCode = qrCode;
    }
    @XmlElement(name="RESP_CODE")
    public String getRespCode() {
        return respCode;
    }
    public void setRespCode(String respCode) {
        this.respCode = respCode;
    }
    @XmlElement(name="RESP_DESC")
    public String getRespDesc() {
        return respDesc;
    }
    public void setRespDesc(String respDesc) {
        this.respDesc = respDesc;
    }
}

TRXMessage.java

@XmlRootElement(name="TRX_MESSAGE")
public class TRXMessage {

    private Message message;

    @XmlElement(name="MESSAGE")
    public Message getMessage() {
        return message;
    }

    public void setMessage(Message message) {
        this.message = message;
    }
}

用jaxb解组

StringReader sr = new StringReader(test);
JAXBContext jaxbContext = JAXBContext.newInstance(TRXMessage.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
TRXMessage response = (TRXMessage) unmarshaller.unmarshal(sr);