开发者

XStream - loading custom 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>
            2259开发者_如何转开发58
        </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.


To process the @XStreamImplicit annotation in MyCompany you need to call this first:

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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜