JAXB:具有2个名称空间的解组对象将导致空值

时间:2018-07-20 02:04:16

标签: java null jaxb

当我尝试解组XML文件时,我得到的是空值, 我创建了具有2个名称空间的package-info.java类,如下所述。 请提出如何解决此问题的建议

1。我的XML文件如下所示:它具有2个名称空间

 <?xml version="1.0" encoding="UTF-8"?>
        <saleResponse xmlns="http://tripos.vantiv.com/2014/09/TriPos.Api" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <_type>saleResponse</_type>
        </saleResponse>

2。我已经像下面这样声明了package-info.java

 @XmlSchema(
                    elementFormDefault=XmlNsForm.QUALIFIED,     
                            xmlns={
                               @XmlNs(prefix="", namespaceURI="http://tripos.vantiv.com/2014/09/TriPos.Api"), 
                               @XmlNs(prefix="i", namespaceURI="http://www.w3.org/2001/XMLSchema-instance")
                           }
                    )
            @XmlAccessorType(XmlAccessType.FIELD)
            package test1;   

            import javax.xml.bind.annotation.XmlNsForm;
            import javax.xml.bind.annotation.XmlSchema;
            import javax.xml.bind.annotation.*;

3。 SaleResponse类为:

        package test1;
        import javax.xml.bind.annotation.XmlElement;
        import javax.xml.bind.annotation.XmlRootElement;

        @XmlRootElement(name = "saleResponse", namespace = "http://tripos.vantiv.com/2014/09/TriPos.Api")
        public class SaleResponse {

            @XmlElement(name = "_type")
            public String _type;


        }

4。当我尝试解组XML文件时得到空值

        package test1;

        import java.io.File;
        import javax.xml.bind.JAXBContext;
        import javax.xml.bind.JAXBException;
        import javax.xml.bind.Unmarshaller;

        public class JAXBExample {
            public static void main(String[] args) {

             try {

                File file = new File("C:\\Ravi\\file.xml");
                JAXBContext jaxbContext = JAXBContext.newInstance(SaleResponse.class);

                Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
                SaleResponse saleResponse = (SaleResponse) jaxbUnmarshaller.unmarshal(file);
                System.out.println(saleResponse._type);

              } catch (JAXBException e) {
                e.printStackTrace();
              }

            }
        }



        **I am getting null value when i try to unmarshal XML file ,
        I have created package-info.java class with 2 name spaces as explained below.
        Please suggest how to fix this issue**

1 个答案:

答案 0 :(得分:0)

您必须将默认的namespace="http://tripos.vantiv.com/2014/09/TriPos.Api"属性添加到package-info.java中,并且还可以删除空前缀之一:@XmlNs(prefix="", namespaceURI="http://tripos.vantiv.com/2014/09/TriPos.Api")

它应该可以正常工作,输出为:saleResponse

package-info.java

@XmlSchema(elementFormDefault = XmlNsForm.QUALIFIED, namespace="http://tripos.vantiv.com/2014/09/TriPos.Api", xmlns = {
        @XmlNs(prefix = "i", namespaceURI = "http://www.w3.org/2001/XMLSchema-instance") })
@XmlAccessorType(XmlAccessType.FIELD)
package test1;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
import javax.xml.bind.annotation.*;

有关更多信息:https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/annotation/XmlSchema.html

相关问题