XStream - 加载自定义 XML

发布于 2024-11-03 20:14:46 字数 1813 浏览 0 评论 0原文

我正在尝试加载以下格式的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉殇 2024-11-10 20:14:46

要处理 MyCompany 中的 @XStreamImplicit 注释,您需要先调用此方法:

m_XStream.processAnnotations(MyCompany.class);

或者您可以执行以下操作而不是注释:

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

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");
楠木可依 2024-11-10 20:14:46

设法通过使用 @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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文