使用Java将RSS提要XML转换为JSON只显示最后一个条目
假设我有一个RSS提要(XML格式),看起来是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
<channel>
<title>MyFeed</title>
<atom:link href="http://samplefeed.com/feed/" rel="self"
type="application/rss+xml" />
<link>http://samplefeed.com</link>
<description></description>
<lastBuildDate>Fri, 22 Sep 2017 22:43:51 +0000</lastBuildDate>
<language>en-US</language>
<sy:updatePeriod>hourly</sy:updatePeriod>
<sy:updateFrequency>1</sy:updateFrequency>
<generator>http://wordpress.org/?v=4.3.12</generator>
<item>
<title>A Good Product</title>
<link>http://samplefeed.com/a-good-product/</link>
<comments>http://samplefeed.com/a-good-product/#comments</comments>
<pubDate>Wed, 20 Sep 2017 22:22:45 +0000</pubDate>
<dc:creator><![CDATA[John Smith]]></dc:creator>
<category><![CDATA[Entertainment]]></category>
<guid isPermaLink="false">http://samplefeed.com/?p=9116</guid>
<description>
<![CDATA[<p>![CDATA[<p>9/22</p>
<p>4K TV Samsung<br />
Price: $500.00<br />
Location: Walmart</p>
]]>
</description>
</item>
<!-- More items -->
</channel>
</rss>
使用了varren的建议,但它返回的是最后一个条目,而不是所有条目
public static void main(String[] args) throws MalformedURLException, IOException {
XmlMapper xmlMapper = new XmlMapper(); // <- This is line 21
ObjectMapper jsonMapper = new ObjectMapper();
InputStream xml = getInputStreamFromUrlData("http://samplefeed.com/feed");
String json = jsonMapper.writeValueAsString(xmlMapper.readTree(xml));
System.out.println(json);
}
public static InputStream getInputStreamForURLData(String Url) {
URL url = null;
HttpURLConnection httpConnection = null;
InputStream content = null;
try {
url = new URL(Url);
System.out.println("URL: " + Url);
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent", "Mozilla/5.0");
httpConnection = (HttpURLConnection) conn;
int responseCode = httpConnection.getResponseCode();
System.out.println("Response Code : " + responseCode);
content = (InputStream) httpConnection.getInputStream();
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
return content;
}
波姆。xml:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.7.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.10</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.7.9</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-xc</artifactId>
<version>1.7.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.9.1</version>
</dependency>
现在的问题是,它只返回RSS提要中的最后一个条目,而不是所有条目?!?它确实将其生成为JSON,但只显示最后一个条目
为什么它不显示所有条目
有人能提出不同的方法吗(例如,如何使用Rome或straight DOM)
# 1 楼答案
Why doesn't it display all of entries?
XML对“对象”和“数组”的概念没有区别,因此只打印单个
item
,而不是所有项目这是修复方法,除了您使用的依赖项之外,我还使用了另外两个JAR,它们如下:-
您的代码:
示例运行:(检查
item
是一个对象数组,而不是单个对象)# 2 楼答案
让它像这样工作:
# 3 楼答案
还有人在使用Jackson将xml转换为json时遇到问题,只得到了最后一个元素:Converting xml to json using jackson
所以你可以试试JSON in Java而不是杰克逊:
示例代码
另见:Quickest way to convert XML to JSON in Java
# 4 楼答案
可以将Jackson与xml依赖项一起使用:
简单的用例如下所示(实际上,您可以使用自定义POJO和大量注释或手动将xml属性/前缀映射到某些json值来定制几乎所有内容):
结果如下: