为这个xml文件写pojo类

时间:2014-11-25 15:16:29

标签: java spring spring-mvc xml-parsing

在将xml文件解组为java类时,我得到null值。但是xml文件作为与其属性对应的值。在我的pojo课程中还是unmarshelling有任何错误吗? 请帮忙

这是我的pojo

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
@XmlRootElement(name = "CardUpdateResponse",namespace="http://www.samople.com/Prepaid")
public class FVCardUpdateResponse {

    @XmlElement(name = "AccountNumber")
    private String AccountNumber;

    @XmlElement(name = "ResCode")
    private String ResCode;

    @XmlElement(name = "ResErrorCode")
    private String ResErrorCode;

    @XmlElement(name = "ResErrorMsg")
    private String ResErrorMsg;

    //Setters and Getters
}

这是我的xml文件

<?xml version="1.0" encoding="UTF-8"?>
<CardUpdateResponse xmlns="http://www.samople.com/Prepaid">
    <CARDUPDATE_RET>
        <ResErrorMsg>ID Issue Date must be equal or less than present date</ResErrorMsg>
        <ResErrorCode>ErrIsud01</ResErrorCode>
        <ResCode>0</ResCode>
        <ACCOUNTNUMBER>2000000003918246</ACCOUNTNUMBER>
    </CARDUPDATE_RET>
</CardUpdateResponse>

这是用于解组的代码

public class XmlUnmarshelling {

    public void unmarshell()
    {

        try
        {
            System.out.println("xml unmarshelling class");
            File file = new File("D:/var/lib/tomcat7/webapps/tmpFiles/1.xml");

            JAXBContext jaxbContext = JAXBContext.newInstance(FVCardUpdateResponse.class);

            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            FVCardUpdateResponse CARDUPDATE_ret = (FVCardUpdateResponse) jaxbUnmarshaller.unmarshal(file);
            System.out.println("xml unmarshelled = "+CARDUPDATE_ret.getResErrorMsg());//Getting null value as response.
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

您的POJO与XML的结构不同。 CardUpdateResponse并不直接包含POJO中的属性,它包含CARDUPDATE_RET元素,其中包含属性。

您可以像这样修改您的POJO以匹配XML:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "CardUpdateResponse", namespace="http://www.samople.com/Prepaid")
public static class CardUpdateResponseWrapper {

    @XmlElement(name="CARDUPDATE_RET")
    private FVCardUpdateResponse response;

    // Getter and setter for response

    public static class FVCardUpdateResponse {

        @XmlElement(name = "AccountNumber")
        private String AccountNumber;

        @XmlElement(name = "ResCode")
        private String ResCode;

        @XmlElement(name = "ResErrorCode")
        private String ResErrorCode;

        @XmlElement(name = "ResErrorMsg")
        private String ResErrorMsg;

        // Getters and setters
    }

}

现在CardUpdateResponseWrapper类将代表您的根XML元素,它将具有代表FVCardUpdateResponse XML元素的CARDUPDATE_RET实例。

取消联系,只需致电:

File file = new File("D:/var/lib/tomcat7/webapps/tmpFiles/1.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(CardUpdateResponseWrapper.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
CardUpdateResponseWrapper wrapper = (CardUpdateResponseWrapper) jaxbUnmarshaller.unmarshal(file);

 System.out.println(wrapper.getResponse().getResErrorMsg());

答案 1 :(得分:1)

我认为这个问题是两个问题的结合,一个是Bohuslav所说的,另一个是你需要在每个XmlElement注释上重复你的命名空间,例如:

 @XmlElement(name = "ResCode", namespace="http://www.samople.com/Prepaid")

和一个特定问题,您需要匹配案例,因此AccountNumber的名称应大写ACCOUNTNUMBER