简体   繁体   中英

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>
            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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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