【发布时间】:2014-02-28 23:48:16
【问题描述】:
我正在使用 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"><AccountInfo AccountID="640480"><Location LocationID="1490075"/><Location LocationID="8900561"/><Location LocationID="2367782"/><Location LocationID="2226598"/></AccountInfo></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
【问题讨论】:
-
可能想要关闭 com 部分以保持匿名。另外,嗨!
标签: java xml scala unmarshalling jaxb2