【问题标题】:Write pojo class for this xml file为这个 xml 文件编写 pojo 类
【发布时间】:2014-11-25 15:16:28
【问题描述】:

将 xml 文件解组为 java 类时,我得到了 null 值。但是 xml 文件作为值对应于它的属性。我的 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();
        }
    }
}

【问题讨论】:

    标签: java spring spring-mvc xml-parsing


    【解决方案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 的实例,该实例将代表 CARDUPDATE_RET XML 元素。

    解组它,只需调用:

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

    【讨论】:

    • 谢谢你们。我结合了两个答案并得到了我想要的确切输出。非常感谢。
    【解决方案2】:

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

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

    还有一个特殊问题,您还需要匹配大小写,因此 AccountNumber 的名称应大写 ACCOUNTNUMBER

    【讨论】:

    • 谢谢你们。我结合了两个答案并得到了我想要的确切输出。非常感谢。
    猜你喜欢
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    • 2012-03-09
    • 2011-06-14
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多