首页 / JAVA / java-解组时发生意外的元素错误
java-解组时发生意外的元素错误
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-解组时发生意外的元素错误,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3396字,纯文字阅读大概需要5分钟。
内容图文
![java-解组时发生意外的元素错误](/upload/InfoBanner/zyjiaocheng/676/704cf604dd69450ba8fb8a552dc1497f.jpg)
我应该将简单的xml解组为belo,但由于出现错误
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"http://example.com/service/response/v1", local:"WebResponse"). Expected elements are <{http://example.com/service/request/v1}WebResponse>
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:603)
I tried the solutions in this site but could not solve pls help.
这是response.xml文件中存在的xml
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
- <WebResponse xmlns="http://example.com/service/response/v1">
- <Status>
<StatusCode>0</StatusCode>
<Description>Transaction was successful</Description>
</Status>
</WebResponse>
下面是我的代码:
WebResponse类:
用于存储检索到的xml的webresponse类
import javax.xml.bind.annotation.*;
@XmlRootElement(name="WebResponse")
public class WebResponse {
private long statusCode;
private String description;
@XmlElement(name= "StatusCode")
public long getStatusCode() {
return statusCode;
}
public void setStatusCode(long statusCode) {
this.statusCode = statusCode;
}
@XmlElement(name= "Description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
WebResponseMain类:
测试员班
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.JAXBContext;
import java.io.FileReader;
import com.example.WebResponse;
public class WebResponseMain {
public static void main(String[] args) throws Exception{
JAXBContext context = JAXBContext.newInstance(WebResponse.class);
Unmarshaller um = context.createUnmarshaller();
WebResponse WR = (WebResponse) um.unmarshal(new FileReader("c:/tem/Response.XML"));
System.out.println("StatusCode: " + WR.getStatusCode() + " Description "
+WR.getDescription());
}
}
package-info.java:
@XmlSchema(namespace = "http://example.com/service/request/v1",elementFormDefault=XmlNsForm.QUALIFIED)
package com.example;
import javax.xml.bind.annotation.*;
我使用了本网站上存在的解决方案,但无法解决,请帮助
解决方法:
第1部分
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"http://example.com/service/response/v1", local:"WebResponse"). Expected elements are <{http://example.com/service/request/v1}WebResponse>
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:603)
问题在于XML中指定的名称空间(response / v1和request / v1).
<WebResponse xmlns="http://example.com/service/response/v1">
和package-info不同
@XmlSchema(namespace = "http://example.com/service/request/v1", elementFormDefault = XmlNsForm.QUALIFIED)
package com.example;
第2部分
您当前的对象模型和映射与XML内容不匹配.解决此问题的一种方法是引入状态类as answered by Ash.
状态
import javax.xml.bind.annotation.XmlElement;
public class Status {
private long statusCode;
private String description;
@XmlElement(name = "StatusCode")
public long getStatusCode() {
return statusCode;
}
public void setStatusCode(long statusCode) {
this.statusCode = statusCode;
}
@XmlElement(name = "Description")
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
Web响应
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "WebResponse")
public class WebResponse {
private Status status;
@XmlElement(name="Status")
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
}
内容总结
以上是互联网集市为您收集整理的java-解组时发生意外的元素错误全部内容,希望文章能够帮你解决java-解组时发生意外的元素错误所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。