簡體   English   中英

為什么我在xml中從java獲取null,即使xml中存在值?

[英]Why am i getting null in java from xml even though value is present in xml?

我試圖為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>

當我嘗試從用戶標簽登錄時,即使值已經存在,我的poji看起來像是

顧客

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)    

請提前幫助謝謝。

您的客戶ID為空,因為它不是@XmlElement。 請改用@XmlAttribute。

為了澄清:

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

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

檢查https://howtodoinjava.com/jaxb/jaxb-annotations/或其他Jaxb教程以獲取其他注釋。

編輯:
對於登錄和電子郵件,您還必須使用@XmlAttribute對它們進行注釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM