[英]JaxB unmarshal custom xml
I'm currently attempting to unmarshal some existing XML into a few classes I have created by hand. 我目前正在尝试将一些现有的XML解组到我手工创建的几个类中。 Problem is, I always get an error that tells me, JaxB expects a weather element but finds a weather element. 问题是,我总是收到一个错误消息,告诉我,JaxB期望使用天气元素,但是找到了天气元素。 (?) (?)
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.aws.com/aws", local:"weather"). javax.xml.bind.UnmarshalException:意外元素(uri:“ http://www.aws.com/aws”,本地:“天气”)。 Expected elements are <{}api>,<{}location>,<{}weather> 期望的元素是<{} api>,<{} location>,<{} weather>
I tried with and without "aws:" in the elements' name. 我尝试过在元素名称中是否包含“ aws:”。
Here's my weather class: 这是我的天气课:
@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;
}
}
And that's the XML I try to parse: 这就是我尝试解析的XML:
<?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>
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. 我怀疑问题是xmlns定义,但我不知道该怎么办。 (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. (您可以通过查看uri属性来看到。这是一个不成功的想法。^^)是的,我的确尝试设置名称空间,但是设置的是名称空间的uri而不是...名称。
I would recommend adding a package-info
class in with your domain model with the @XmlSchema
annotation to specify the namespace qualification: 我建议您在带有@XmlSchema
批注的域模型中添加一个package-info
类,以指定名称空间限定:
package-info 包装信息
@XmlSchema(
namespace = "http://www.aws.com/aws",
elementFormDefault = XmlNsForm.QUALIFIED)
package com.example.foo;
import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
Note 注意
Your XmlRootElement
and @XmlElement
annotation should not contain the namespace prefix. 您的XmlRootElement
和@XmlElement
批注不应包含名称空间前缀。 You should have @XmlRootElement(name = "weather")
instead of @XmlRootElement(name = "aws:weather")
您应该使用@XmlRootElement(name = "weather")
而不是@XmlRootElement(name = "aws:weather")
For More Information 欲获得更多信息
you need namespaces in your code. 您的代码中需要名称空间。 namespace prefixes are meaningless, you need the actual namespace (ie "http://www.aws.com/aws"). 名称空间前缀是没有意义的,您需要实际的名称空间(即“ http://www.aws.com/aws”)。
@XmlRootElement(name = "weather", namespace="http://www.aws.com/aws")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.