XStream - 加载自定义 XML
我正在尝试加载以下格式的 XML 文件:
<?xml version="1.0" encoding="ISO-8859-1"?>
<MyCompany>
<Record>
<Surname>
Bird
</Surname>
<Given1>
Andrew
</Given1>
<ID>
225958
</ID>
<BirthDate>
260391
</BirthDate>
<PeerYear>
2009
</PeerYear>
<Title>
</Title>
<Preferred>
Andrew
</Preferred>
<Given2>
Macarthur
</Given2>
<CountryOfBirthCode>
AUS
</CountryOfBirthCode>
<NationalityCode>
</NationalityCode>
<OccupCode>
Retired
</OccupCode>
<Suburb>
Metung
</Suburb>
<State>
Vic
</State>
<PostCode>
3904
</PostCode>
<CountryCode>
AUS
</CountryCode>
<Phone>
</Record>
我尝试读取,而不是写入该格式,因此我将别名设置为:
m_XStream.alias("MyCompany", MyCompany.class);
m_XStream.alias("Record", Record.class);
Where Mycompany is:
public class Mycompany
{
@XStreamImplicit
public List<Record> Records = new ArrayList<Record>();
}
And Record is a class with public member variables ala:
public class Record
{
public String ID;
public String Surname;
}
当我尝试从上面的 XML 中读取时,它不会读取任何内容MyCompany.Records 成员变量。
像这样读取 XML 的正确方法是什么,以及如何忽略不存在成员变量的元素?
谢谢。
I'm trying to load an XML file of the following format:
<?xml version="1.0" encoding="ISO-8859-1"?>
<MyCompany>
<Record>
<Surname>
Bird
</Surname>
<Given1>
Andrew
</Given1>
<ID>
225958
</ID>
<BirthDate>
260391
</BirthDate>
<PeerYear>
2009
</PeerYear>
<Title>
</Title>
<Preferred>
Andrew
</Preferred>
<Given2>
Macarthur
</Given2>
<CountryOfBirthCode>
AUS
</CountryOfBirthCode>
<NationalityCode>
</NationalityCode>
<OccupCode>
Retired
</OccupCode>
<Suburb>
Metung
</Suburb>
<State>
Vic
</State>
<PostCode>
3904
</PostCode>
<CountryCode>
AUS
</CountryCode>
<Phone>
</Record>
I try to READ, not write to that format, so I've set aliases as:
m_XStream.alias("MyCompany", MyCompany.class);
m_XStream.alias("Record", Record.class);
Where Mycompany is:
public class Mycompany
{
@XStreamImplicit
public List<Record> Records = new ArrayList<Record>();
}
And Record is a class with public member variables ala:
public class Record
{
public String ID;
public String Surname;
}
When I try to read from above XML, it doesn't read anything into MyCompany.Records member variable.
What would be the correct way to read XML like that and, also, how to ignore elements for which member variable is not present?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要处理
MyCompany
中的@XStreamImplicit
注释,您需要先调用此方法:或者您可以执行以下操作而不是注释:
To process the
@XStreamImplicit
annotation inMyCompany
you need to call this first:Or instead of the annotation you can do this:
设法通过使用 @XStreamImplicit(itemFieldName="Record") 而不是 m_XStream.addImplicitCollection(MyCompany.class, "Records", Record.class) 来解决它
Managed to solve it by using @XStreamImplicit(itemFieldName="Record") instead of m_XStream.addImplicitCollection(MyCompany.class, "Records", Record.class)