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

1.8k Views Asked by At

Im trying to build java object for xml by

java code

 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>

when I try to get login from user tag even though value is there im getting null my poji looks like

Customer

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;
    }


    
}

This is users POJO class

Users

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;
}
}

this is the user POJO class

user

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;
}

}

Im getting value in id but not in other variable refer the 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)    

please help thanks in advance.

1

There are 1 best solutions below

1
second On

You are getting null for the Id of your customer because it's not an @XmlElement. Use @XmlAttribute instead.

For clarification:

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

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

Check https://howtodoinjava.com/jaxb/jaxb-annotations/ or other Jaxb tutorials for other annotations.

Edit:
For login and email, you have to annotate them with @XmlAttribute as well.