为什么即使xml中存在值,我也无法从xml中获取Java中的null?

时间:2019-05-29 17:04:57

标签: java xml jaxb nsxmlelement

我正在尝试通过

为xml构建Java对象

java代码

 JAXBContext jaxbContext = JAXBContext.newInstance(Enfinity.class);  
         Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
         Enfinity enfinity = (Enfinity) jaxbUnmarshaller.unmarshal(xmlFile); 

xml

<?xml version="1.0" encoding="UTF-8"?>
<enfinity xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dt="http://www.intershop.com/xml/ns/enfinity/6.5/core/impex-dt" xsi:schemaLocation="http://www.intershop.com/xml/ns/intershop/customer/impex/7.3 b2b_customer.xsd http://www.intershop.com/xml/ns/enfinity/6.5/core/impex-dt dt.xsd" major="6" minor="1" family="enfinity" branch="enterprise" build="1.0.299">
    <customer id="34627">
    <customer-type>SMB</customer-type>
    <company-name>xxxxx &amp; xxxxx</company-name>
    <industry>Retail</industry>
    <enabled>1</enabled>
    <approval-status>1</approval-status>
    <custom-attributes>
    <custom-attribute name="CustomerPriceLevel" dt:dt="string">4</custom-attribute>
    <custom-attribute name="FreeFreightThreshold" dt:dt="string">300.00</custom-attribute>
    <custom-attribute name="ECOMCustomerId" dt:dt="string">xxxxxx</custom-attribute>
    <custom-attribute name="BlockCreditCardPayment" dt:dt="boolean">true</custom-attribute>
    <custom-attribute name="CustomLoad" dt:dt="string">true</custom-attribute>
    </custom-attributes>
    <users>
    <user business-partner-no="xxxx">
    <business-partner-no>xxxxx</business-partner-no>
    <profile>
    <first-name>xxxx</first-name>
    <last-name>xxxx</last-name>
    <email>xxx</email>
    </profile>
    <user-groups>
    <user-group id="IG_SMBCustomers"/>
    <user-group id="IG_RecurringUsers"/>
    <user-group id="52"/>
    </user-groups>
    </user>
    <user business-partner-no="xxxxx">
    <business-partner-no>xxxxx</business-partner-no>
    <profile>
    <credentials>
    <login>xxxxx.com</login>
    <password encrypted="1">xxxxx</password>
    <enabled>1</enabled>
    <reminder-email>xxxxxx.com</reminder-email>
    <password-creation-date>2019-03-28T06:37:29-07:00</password-creation-date>
    </credentials>
    <creation-date>2019-02-28T03:45:43-08:00</creation-date>
    <phone-home>xxxxxx</phone-home>
    <email>xxxxxxxxx.com</email>
    <last-name>xxxxx</last-name>
    <first-name>xxxxxx</first-name>
    <custom-attributes>
    <custom-attribute name="RoleID" dt:dt="string">APP_B2B_BUYER</custom-attribute>
    </custom-attributes>
    </profile>
    </user>
    </users>

当我尝试从用户标签获取登录名时,即使值仍然存在,但我的null却变成null

客户

package com.poc.highline;


import java.util.List;

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

@XmlRootElement(name = "customer")
@XmlAccessorType(XmlAccessType.FIELD)
public class Customer {

    @XmlElement (name = "id")
    String id;
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @XmlElement (name = "customer-type")
    String customerType;
    public String getCustomerType() {
        return customerType;
    }

    public void setCustomerType(String customerType) {
        this.customerType = customerType;
    }

    @XmlElement (name = "company-name")
    String companyName;
    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    @XmlElement (name = "industry")
    String industry;
    public String getIndustry() {
        return industry;
    }

    public void setIndustry(String industry) {
        this.industry = industry;
    }

    @XmlElement (name = "enabled")
    String enabled;
    public String getEnabled() {
        return enabled;
    }

    public void setEnabled(String enabled) {
        this.enabled = enabled;
    }

    @XmlElement (name = "approval-status")
    String approvalStatus;
    public String getApprovalStatus() {
        return approvalStatus;
    }

    public void setApprovalStatus(String approvalStatus) {
        this.approvalStatus = approvalStatus;
    }

    @XmlElement (name = "custom-attributes")
     List<customAttributes> customAttributes;
    public List<customAttributes> getCustomAttributes() {
        return customAttributes;
    }

    public void setCustomAttributes(List<customAttributes> customAttributes) {
        this.customAttributes = customAttributes;
    }

    @XmlElement (name = "users")
    List<Users> users;
    public List<Users> getUsers() {
        return users;
    }

    public void setUsers(List<Users> users) {
        this.users = users;
    }

    @XmlElement (name = "preferred-invoice-to-address")
    PreferredInvoiceAddress invoiceAddress;
    public PreferredInvoiceAddress getInvoiceAddress() {
        return invoiceAddress;
    }

    public void setInvoiceAddress(PreferredInvoiceAddress invoiceAddress) {
        this.invoiceAddress = invoiceAddress;
    }

    @XmlElement (name = "addresses")
    List<Addresses> addresses;
    public List<Addresses> getAddresses() {
        return addresses;
    }

    public void setAddresses(List<Addresses> addresses) {
        this.addresses = addresses;
    }



}

这是用户POJO类

用户

package com.poc.highline;

import java.util.List;

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

@XmlRootElement(name = "users")
@XmlAccessorType(XmlAccessType.FIELD)
public class Users {


@XmlElement(name="user")    
List<User> user;
public List<User> getUser() {
    return user;
}

public void setUser(List<User> user) {
    this.user = user;
}
}

这是用户POJO类

用户

package com.poc.highline;

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

@XmlRootElement(name = "user")
@XmlAccessorType(XmlAccessType.FIELD)
public class User {

@XmlElement(name = "business-partner-no")
String id;
public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

@XmlElement(name = "profile")
String profile;
public String getProfile() {
    return profile;
}

public void setProfile(String profile) {
    this.profile = profile;
}


@XmlElement(name = "credentials")
String credentials;

public String getCredentials() {
    return credentials;
}

public void setCredentials(String credentials) {
    this.credentials = credentials;
}


String login;
public String getLogin() {
    return login;
}

public void setLogin(String login) {
    this.login = login;
}



String email;
public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

}

我在id中获取值,但在其他变量中未获取值,请参考watchList

enfinity    Enfinity  (id=30)   
    customer    ArrayList<E>  (id=32)   
        [0] Customer  (id=43)   
            addresses   ArrayList<E>  (id=45)   
            approvalStatus  "1" (id=46) 
            companyName "xxxxx & xxxxx" (id=49) 
            customAttributes    ArrayList<E>  (id=50)   
            customerType    "SMB" (id=51)   
            enabled "1" (id=52) 
            id  null    
            industry    "Retail" (id=53)    
            invoiceAddress  PreferredInvoiceAddress  (id=54)    
            users   ArrayList<E>  (id=56)   
                [0] Users  (id=58)  
                    user    ArrayList<E>  (id=60)   
                        [0] User  (id=62)   
                            credentials null    
                            email   null    
                            id  "xxxxx" (id=65) 
                            login   null    
                            profile "\n    " (id=66)    
                        [1] User  (id=63)   
                            credentials null    
                            email   null    
                            id  "xxxxx" (id=67) 
                            login   null    
                            profile "\n    " (id=68)    

请帮助谢谢。

1 个答案:

答案 0 :(得分:0)

您获得的客户ID为空,因为它不是@XmlElement。请改用@XmlAttribute。

为澄清起见:

<customer id="1">
  <name>somename</name>
<customer>

id: @XmlAttribute
name: @XmlElement
somename: @XmlValue

检查https://howtodoinjava.com/jaxb/jaxb-annotations/或其他Jaxb教程中是否有其他注释。

编辑:
对于登录名和电子邮件,还必须使用@XmlAttribute对其进行注释。