saxparseexception元素类型“ hr”必须由匹配的末端标签终止。与JAXB一起阅读XML时
我正在尝试阅读以下 xml 来自给予链接与jaxb。我不断得到以下例外。文档中没有人力资源标签。
这是我的代码:
final JAXBContextjaxbContext=JAXBContext.newInstance(EuropeanParliamentMemberResponse.class);
final Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
final JAXBElement<EuropeanParliamentMemberResponse> response = jaxbUnmarshaller.unmarshal(new StreamSource(url), EuropeanParliamentMemberResponse.class);
这是例外:
org.xml.sax.SAXParseException; systemId: http://www.europarl.europa.eu/meps/en/full-list/xml; lineNumber: 6; columnNumber: 3; The element type "hr" must be terminated by the matching end-tag "</hr>".]
我做错了什么?
I am trying to read the following xml from the giving link with jaxb. I keep getting the following exception. There is no hr tag in the document.
here is my code:
final JAXBContextjaxbContext=JAXBContext.newInstance(EuropeanParliamentMemberResponse.class);
final Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
final JAXBElement<EuropeanParliamentMemberResponse> response = jaxbUnmarshaller.unmarshal(new StreamSource(url), EuropeanParliamentMemberResponse.class);
Here is the Exception:
org.xml.sax.SAXParseException; systemId: http://www.europarl.europa.eu/meps/en/full-list/xml; lineNumber: 6; columnNumber: 3; The element type "hr" must be terminated by the matching end-tag "</hr>".]
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您遇到该错误的原因是因为您在URL中使用了错误的协议。使用
HTTPS
而不是http
。当您使用
http
时,服务器会生成“ 301-永久移动”响应:您可以看到
&lt; hr&gt;
tag引起错误(它对该错误无效) XML的预期内容类型)。如果您使用
http
url,您的浏览器将正确处理此操作 - 但是您的JAXB Unmarshaller不会。假设您在课堂上有所有正确的JAXB注释,则问题中的代码应使用更新的URL起作用(对我有效):
有几个建议解决此类问题的建议:
转到主页,请访问主页。浏览器:
http://www.europarl.europa.eu
- 您会看到您已重定向到https
url。您可以通过使用Java的
httpclient
提取上面显示的重定向响应。 =“ nofollow noreferrer”>可从Java 11开始):这是打印响应主体,您可以在其中看到重定向消息。
The reason you are getting that error is because you are using the wrong protocol in your URL. Use
https
instead ofhttp
.When you use
http
, the server generates a "301 - moved permanently" response:You can see the
<hr>
tag causing the error (it is not valid for the expected content type of XML).Your browser will handle this correctly, if you use the
http
URL - but your JAXB unmarshaller will not.Assuming you have all the correct JAXB annotations on your class, the code in your question should work (it works for me) with the updated URL:
A couple of suggestions for troubleshooting this type of issue:
Go to the home page in a browser:
http://www.europarl.europa.eu
- and you will see that you are redirected to ahttps
URL.You can extract the redirect response I showed above by using Java's
HttpClient
(available from Java 11 onwards):This prints the response body, where you can see the redirect message.