我正在使用JAXB2将我的XML字符串解组为名为AccountInfo的java对象。 AccountInfo包含accountID和Location对象列表。目前我可以从xml中提取acountID,但我的位置列表始终为null。任何帮助将不胜感激!格拉西亚斯!
这是我的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
答案 0 :(得分:1)
该文档试图解组与您的XML架构不匹配。
<AccountInfo AccountID="640480">
<Location LocationID="1490075"/>
<Location LocationID="8900561"/>
<Location LocationID="2367782"/>
<Location LocationID="2226598"/>
</AccountInfo>
要使其与XML架构(请参阅targetNamespace
和elementFormDefault
属性)以及您需要的@XmlSchema
类的package-info
注释中的JAXB映射(请参阅<AccountInfo xmlns="http://www.cspire.com/omnia/schema" AccountID="640480">
<Location LocationID="1490075"/>
<Location LocationID="8900561"/>
<Location LocationID="2367782"/>
<Location LocationID="2226598"/>
</AccountInfo>
注释)匹配声明命名空间信息。
{{1}}
调试提示
如果无法让JAXB解组XML文档,请尝试填充对象模型并对其进行编组以查看JAXB期望的XML文档。
答案 1 :(得分:0)
我获取位置列表 - 这是代码。
String xml ="" +
" <AccountInfo xmlns=\"http://www.cspire.com/omnia/schema\" AccountID=\"640480\"> " +
" <Location LocationID=\"1490075\"/> " +
" <Location LocationID=\"8900561\"/> " +
" <Location LocationID=\"2367782\"/> " +
" <Location LocationID=\"2226598\"/> " +
" </AccountInfo> " +
"";
try {
String testXML = xml ;
ByteArrayInputStream inputXml = new ByteArrayInputStream (testXML.getBytes());
JAXBContext jc = JAXBContext.newInstance(AccountInfo.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
AccountInfo result;
result = (AccountInfo) unmarshaller.unmarshal(inputXml);
System.out.println("Account ID: " + result.getAccountID());
System.out.println(result.getLocation().size());
for(Location loc: result.getLocation()){
System.out.println(loc.getLocationID());
}
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
输出:
Account ID: 640480
4
1490075
8900561
2367782
2226598