JaxB unmarshal自定义xml [英] JaxB unmarshal custom xml

查看:91
本文介绍了JaxB unmarshal自定义xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试将一些现有的XML解组为我手工创建的几个类。问题是,我总是得到一个错误,告诉我,JaxB期望一个天气元素,但找到一个天气元素。 (?)

I'm currently attempting to unmarshal some existing XML into a few classes I have created by hand. Problem is, I always get an error that tells me, JaxB expects a weather element but finds a weather element. (?)


javax.xml.bind.UnmarshalException:意外元素(uri:http://www.aws.com/aws ,当地:天气)。预期元素为< {} api>,< {} location>,< {} weather>

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.aws.com/aws", local:"weather"). Expected elements are <{}api>,<{}location>,<{}weather>

我试过和元素名称中没有aws:。

I tried with and without "aws:" in the elements' name.

这是我的天气等级:

@XmlRootElement(name = "aws:weather")
public class WeatherBugWeather
{
    private WeatherBugApi api;
    private List<WeatherBugLocation> locations;
    private String uri;

    @XmlElement(name="aws:api")
    public WeatherBugApi getApi()
    {
        return this.api;
    }

    @XmlElementWrapper(name = "aws:locations")
    @XmlElement(name = "aws:location")
    public List<WeatherBugLocation> getLocations()
    {
        return this.locations;
    }

    public void setApi(WeatherBugApi api)
    {
        this.api = api;
    }

    public void setLocations(List<WeatherBugLocation> locations)
    {
        this.locations = locations;
    }

    @XmlAttribute(name="xmlns:aws")
    public String getUri()
    {
        return this.uri;
    }

    public void setUri(String uri)
    {
        this.uri = uri;
    }
}

这就是我尝试解析的XML:

And that's the XML I try to parse:

<?xml version="1.0" encoding="utf-8"?>
<aws:weather xmlns:aws="http://www.aws.com/aws">
    <aws:api version="2.0" />
    <aws:locations>
        <aws:location cityname="Jena" statename="" countryname="Germany" zipcode="" citycode="59047" citytype="1" />
    </aws:locations>
</aws:weather>

我不太确定我做错了什么。任何提示?我怀疑问题是xmlns定义,但我不知道如何处理它。 (你可以通过查看uri-property看到这一点。这是一个不成功的想法。^^)是的,我确实尝试设置命名空间,而是设置命名空间的uri而不是它的... name。

I'm not quite sure what I'm doing wrong. Any hints? I suspect the problem to be the xmlns definition, but I have no idea what to do about it. (You can see that by looking at the uri-property. That was one unsuccessful idea. ^^) And yes, I did try to set the namespace but that rather set's the namespace's uri instead of it's ... name.

推荐答案

您需要在代码中使用命名空间。名称空间前缀没有意义,你需要实际的名称空间(即http://www.aws.com/aws)。

you need namespaces in your code. namespace prefixes are meaningless, you need the actual namespace (i.e. "http://www.aws.com/aws").

@XmlRootElement(name = "weather", namespace="http://www.aws.com/aws")

这篇关于JaxB unmarshal自定义xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆