简体   繁体   English

XStream - 加载自定义 XML

[英]XStream - loading custom XML

I'm trying to load an XML file of the following format:我正在尝试加载以下格式的 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>

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: Record 是一个带有公共成员变量 ala 的 class:

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.当我尝试从 XML 上方读取时,它不会将任何内容读入 MyCompany.Records 成员变量。

What would be the correct way to read XML like that and, also, how to ignore elements for which member variable is not present?像这样读取 XML 的正确方法是什么,以及如何忽略不存在成员变量的元素?

Thank you.谢谢你。

To process the @XStreamImplicit annotation in MyCompany you need to call this first:要处理MyCompany中的@XStreamImplicit注释,您需要先调用它:

m_XStream.processAnnotations(MyCompany.class);

Or instead of the annotation you can do this:或者代替注释,您可以这样做:

m_XStream.addImplicitCollection(MyCompany.class, "Records");

Managed to solve it by using @XStreamImplicit(itemFieldName="Record") instead of m_XStream.addImplicitCollection(MyCompany.class, "Records", Record.class)设法通过使用 @XStreamImplicit(itemFieldName="Record") 而不是 m_XStream.addImplicitCollection(MyCompany.class, "Records", Record.class) 来解决它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM