简体   繁体   English

解析对对象 java 的文本响应

[英]Parse text response to object java

I have consumed rest Webservice and I get a response in text format, I want to convert this response to java object in order to do some logic.我已经使用了 rest Webservice 并且我得到了文本格式的响应,我想将此响应转换为 java 对象以执行一些逻辑。

I have tried to parse the text to XML unfortunately, doesn't work.不幸的是,我试图将文本解析为 XML,但不起作用。

Response from web service:来自网络服务的响应:

<html>
<title>rest response</title>
<body>
<a href="http://Xwebservice-urlXXX/XY111NWA1">XY111NWA1</a>
<a href="http://Xwebservice-urlXXX/XY112NWA1">XY112NWA1</a>
<a href="http://Xwebservice-urlXXX/XY113NWA1">XY113NWA1</a>
<a href="http://Xwebservice-urlXXX/XY114NWA1">XY114NWA1</a>
<a href="http://Xwebservice-urlXXX/XY115NWA1">XY115NWA1</a>
<a href="http://Xwebservice-urlXXX/XY116NWA1">XY116NWA1</a>
<a href="http://Xwebservice-urlXXX/XY117NWA1">XY117NWA1</a>
<a href="http://Xwebservice-urlXXX/XY118NWA1">XY118NWA1</a>
<a href="http://Xwebservice-urlXXX/XY119NWA1">XY119NWA1</a>
</body>
</html>

I expect the output to be in XML format so I can parse then to JAVA object, or get the date between tags and the result should be a java list as follows:我希望输出是 XML 格式,这样我就可以解析为 JAVA 对象,或者获取标签之间的日期,结果应该是一个 java 列表,如下所示:

XY111NWA1
XY112NWA1
XY113NWA1
XY114NWA1
XY115NWA1
XY116NWA1
XY117NWA1
XY118NWA1
XY119NWA1

That's not a REST response(not a JSON or response formatted XML), so if you need to parse HTML page(or html-like response).这不是 REST 响应(不是 JSON 或响应格式的 XML),因此如果您需要解析 HTML 页面(或类似 html 的响应)。

WAY 1: I would rather recommend such tool as a jsoup:方式1:我更愿意推荐像jsoup这样的工具:

This way you can extract any node/value/attribute from your html response/file.通过这种方式,您可以从 html 响应/文件中提取任何节点/值/属性。

WAY 2 You can try more general SAX or JAXB parsers for parsing xml (even if it's HTML) like in https://www.mkyong.com/java/jaxb-hello-world-example/ .方式 2您可以尝试使用更通用的 SAX 或 JAXB 解析器来解析 xml(即使它是 HTML),例如https://www.mkyong.com/java/jaxb-hello-world-example/ But that's if you have some contract in your response.但前提是你的回应中有一些合同。 And from what I see that's not a best approach here.从我看来,这不是最好的方法。

Relevant link: Which is the best library for XML parsing in java相关链接: java中解析XML最好的库是哪个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM