gpt4 book ai didi

java - 即使值存在于 xml 中,为什么我在 java 中从 xml 得到 null ?

转载 作者:行者123 更新时间:2023-12-02 05:03:53 24 4
gpt4 key购买 nike

我正在尝试通过以下方式为 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 看起来也为 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)

请帮忙提前致谢。

最佳答案

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

澄清:

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

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

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

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

关于java - 即使值存在于 xml 中,为什么我在 java 中从 xml 得到 null ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56365453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com