gpt4 book ai didi

java - 使用 JAXB2 解码 XML 返回空列表 (Scala)

转载 作者:太空宇宙 更新时间:2023-11-04 07:04:59 26 4
gpt4 key购买 nike

我正在使用 JAXB2 将 XML 字符串解码到名为 AccountInfo 的 java 对象中。 AccountInfo 包含一个 accountID 和一个 Location 对象列表。目前,我可以从 xml 中提取 acountID,但我的位置列表始终为空。任何帮助将不胜感激!谢谢!

这是我尝试解码的 xml:

<AccountInfo AccountID="640480">
<Location LocationID="1490075"/>
<Location LocationID="8900561"/>
<Location LocationID="2367782"/>
<Location LocationID="2226598"/>
</AccountInfo>
<小时/>

我的 AccountInfo 架构(自动生成我的 java 对象):

<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.cspire.com/omnia/schema" xmlns:tns="http://www.cspire.com/omnia/schema"
elementFormDefault="qualified">

<xsd:element name="AccountInfo">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Location" type="tns:Location" minOccurs="0" maxOccurs="unbounded" />
</xsd:sequence>
<xsd:attribute name="AccountID" type="xsd:string"></xsd:attribute>
</xsd:complexType>
</xsd:element>

<xsd:complexType name="Location">
<xsd:attribute name="LocationID" type="xsd:string" />
</xsd:complexType>

</schema>
<小时/>

(部分)AccountInfo.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"location"})
@XmlRootElement(name = "AccountInfo")
public class AccountInfo implements Equals, HashCode, ToString
{

@XmlElement(name = "Location")
protected List<Location> location;
@XmlAttribute(name = "AccountID")
protected String accountID;

public AccountInfo() {
super();
}

public AccountInfo(final List<Location> location, final String accountID) {
this.location = location;
this.accountID = accountID;
}

public List<Location> getLocation() {
if (location == null) {
location = new ArrayList<Location>();
}
return this.location;
}

public String getAccountID() {
return accountID;
}

public void setAccountID(String value) {
this.accountID = value;
}
<小时/>

(部分)Location.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Location")
public class Location implements Equals, HashCode, ToString
{
@XmlAttribute(name = "LocationID")
protected String locationID;

public Location() {
super();
}

public Location(final String locationID) {
this.locationID = locationID;
}

public String getLocationID() {
return locationID;
}

public void setLocationID(String value) {
this.locationID = value;
}
<小时/>

我的主要目标:

object Main extends App {
val xml = <string xmlns="http://schemas.martin-group.com/openomnia">&lt;AccountInfo AccountID="640480"&gt;&lt;Location LocationID="1490075"/&gt;&lt;Location LocationID="8900561"/&gt;&lt;Location LocationID="2367782"/&gt;&lt;Location LocationID="2226598"/&gt;&lt;/AccountInfo&gt;</string>
val testXML = xml \\ "string" text

val jc = JAXBContext.newInstance(classOf[AccountInfo])
val unmarshaller = jc.createUnmarshaller
val result = unmarshaller.unmarshal(new StreamSource(new StringReader(testXML), classOf[AccountInfo]).getValue

println(result)
println("Account ID: " + result.getAccountID)
println(result.getLocation.isEmpty)
}
<小时/>

结果:

com.cspire.omnia.schema.AccountInfo@74c3d5ab[location=<null>, accountID=640480]
Account ID: 640480
true

最佳答案

尝试解码的文档与您的 XML 架构不匹配。

<AccountInfo AccountID="640480">
<Location LocationID="1490075"/>
<Location LocationID="8900561"/>
<Location LocationID="2367782"/>
<Location LocationID="2226598"/>
</AccountInfo>

要使其与 XML 架构(请参阅 targetNamespaceelementFormDefault 属性)和 JAXB 映射(请参阅 package-info 类上的 @XmlSchema 注释)匹配,您需要声明命名空间信息。

<AccountInfo xmlns="http://www.cspire.com/omnia/schema" AccountID="640480">
<Location LocationID="1490075"/>
<Location LocationID="8900561"/>
<Location LocationID="2367782"/>
<Location LocationID="2226598"/>
</AccountInfo>
<小时/>

调试提示

当您无法让 JAXB 解编 XML 文档时,请尝试填充对象模型并编码它以查看 JAXB 期望的 XML 文档。

关于java - 使用 JAXB2 解码 XML 返回空列表 (Scala),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21562135/

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