[英]JaxB unmarshal custom xml
我目前正在尝试将一些现有的XML解组到我手工创建的几个类中。 问题是,我总是收到一个错误消息,告诉我,JaxB期望使用天气元素,但是找到了天气元素。 (?)
javax.xml.bind.UnmarshalException:意外元素(uri:“ http://www.aws.com/aws”,本地:“天气”)。 期望的元素是<{} api>,<{} location>,<{} weather>
我尝试过在元素名称中是否包含“ aws:”。
这是我的天气课:
@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:
<?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属性来看到。这是一个不成功的想法。^^)是的,我的确尝试设置名称空间,但是设置的是名称空间的uri而不是...名称。
我建议您在带有@XmlSchema
批注的域模型中添加一个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;
注意
您的XmlRootElement
和@XmlElement
批注不应包含名称空间前缀。 您应该使用@XmlRootElement(name = "weather")
而不是@XmlRootElement(name = "aws:weather")
欲获得更多信息
您的代码中需要名称空间。 名称空间前缀是没有意义的,您需要实际的名称空间(即“ http://www.aws.com/aws”)。
@XmlRootElement(name = "weather", namespace="http://www.aws.com/aws")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.