saxparseexception元素类型“ hr”必须由匹配的末端标签终止。与JAXB一起阅读XML时

发布于 2025-02-06 17:03:11 字数 841 浏览 1 评论 0原文

我正在尝试阅读以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

滴情不沾 2025-02-13 17:03:11

您遇到该错误的原因是因为您在URL中使用了错误的协议。使用HTTPS而不是http

当您使用http时,服务器会生成“ 301-永久移动”响应:

<html>
    <head><title>301 Moved Permanently</title></head>
    <body>
        <center>
            <h1>301 Moved Permanently</h1>
        </center>
        <hr>
        <center>nginx</center>
    </body>
</html>

您可以看到&lt; hr&gt; tag引起错误(它对该错误无效) XML的预期内容类型)。

如果您使用http url,您的浏览器将正确处理此操作 - 但是您的JAXB Unmarshaller不会。

假设您在课堂上有所有正确的JAXB注释,则问题中的代码应使用更新的URL起作用(对我有效):

https://www.europarl.europa.eu/meps/en/full-list/xml

有几个建议解决此类问题的建议:

  1. 转到主页,请访问主页。浏览器:http://www.europarl.europa.eu - 您会看到您已重定向到https url。

  2. 您可以通过使用Java的httpclient提取上面显示的重定向响应。 =“ nofollow noreferrer”>可从Java 11开始):

String url = "http://www.europarl.europa.eu/meps/en/full-list/xml";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create(url))
        .build();
client.sendAsync(request, BodyHandlers.ofString())
        .thenApply(HttpResponse::body)
        .thenAccept(System.out::println)
        .join();

这是打印响应主体,您可以在其中看到重定向消息。

The reason you are getting that error is because you are using the wrong protocol in your URL. Use https instead of http.

When you use http, the server generates a "301 - moved permanently" response:

<html>
    <head><title>301 Moved Permanently</title></head>
    <body>
        <center>
            <h1>301 Moved Permanently</h1>
        </center>
        <hr>
        <center>nginx</center>
    </body>
</html>

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:

https://www.europarl.europa.eu/meps/en/full-list/xml

A couple of suggestions for troubleshooting this type of issue:

  1. Go to the home page in a browser: http://www.europarl.europa.eu - and you will see that you are redirected to a https URL.

  2. You can extract the redirect response I showed above by using Java's HttpClient (available from Java 11 onwards):

String url = "http://www.europarl.europa.eu/meps/en/full-list/xml";
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create(url))
        .build();
client.sendAsync(request, BodyHandlers.ofString())
        .thenApply(HttpResponse::body)
        .thenAccept(System.out::println)
        .join();

This prints the response body, where you can see the redirect message.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文