2017-09-23 20 views
1

可以說我有一個RSS源(這是XML格式)一種看起來像這樣:轉換RSS訂閱XML使用Java JSON來只顯示最後輸入

<?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; 
} 

的pom.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> 

現在的問題是,它的外核層y返回RSS Feed中的最後一項而不是全部?!?它將它生成爲JSON,但只顯示最後一個條目。

爲什麼不顯示所有條目?

任何人都可以提出一種不同的方法(例如如何使用羅馬或直接DOM做到這一點)?

+0

請檢查我的解決方案,它打印所需的輸出即'item'作爲對象的數組,而不是作爲單個對象。 – JRG

回答

0

得到它的工作是這樣的:

@RequestMapping(value = "/v2/convertIntoJson", 
       method = RequestMethod.GET, 
       produces = "application/json") 
public String getRssFeedAsJson() throws IOException { 
    InputStream xml = getInputStreamForURLData("http://www.samplefeed.com/feed/"); 
    byte[] byteArray = IOUtils.toByteArray(xml); 
    String xmlString = new String(byteArray); 
    JSONObject xmlToJsonObject = XML.toJSONObject(xmlString); 
    String jsonString = xmlToJsonObject.toString(); 
    byte[] jsonStringAsByteArray = jsonString.getBytes("UTF-8"); 
    String retValue = new String(jsonStringAsByteArray, "UTF-8"); 
    return retValue; 
} 

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; 
} 
0

可以使用Jackson與XML相關性:

compile 'com.fasterxml.jackson.core:jackson-databind:2.9.1' 
compile 'com.fasterxml.jackson.core:jackson-core:2.9.1' 
compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.1' // <= that 

而簡單的用例樣子(其實你可以自定義幾乎與定製POJO和大量的註釋或manualy地圖XML屬性/前綴的一些JSON值的一切) :

public class Main4 { 
    public static void main(String[] args) throws Exception { 
    XmlMapper xmlMapper = new XmlMapper(); 
    ObjectMapper jsonMapper = new ObjectMapper(); 
    InputStream XML = Main4.class.getClassLoader() 
      .getResourceAsStream("rss.xml"); 
    String json = jsonMapper.writeValueAsString(xmlMapper.readTree(XML)); 
    System.out.println(json); 
    } 
} 

結果:

{ 
    "version": "2.0", 
    "channel": { 
    "title": "MyFeed", 
    "link": "http://samplefeed.com", 
    "description": "", 
    "lastBuildDate": "Fri, 22 Sep 2017 22:43:51 +0000", 
    "language": "en-US", 
    "updatePeriod": "hourly", 
    "updateFrequency": "1", 
    "generator": "http://wordpress.org/?v=4.3.12", 
    "item": { 
     "title": "A Good Product", 
     "link": "http://samplefeed.com/a-good-product/", 
     "comments": "http://samplefeed.com/a-good-product/#comments", 
     "pubDate": "Wed, 20 Sep 2017 22:22:45 +0000", 
     "creator": "John Smith", 
     "category": "Entertainment", 
     "guid": { 
     "isPermaLink": "false", 
     "": "http://samplefeed.com/?p=9116" 
     }, 
     "description": "\n    <p>![CDATA[<p>9/22</p>\n    <p>4K TV Samsung<br />\n    Price: $500.00<br />\n    Location: Walmart</p>\n    \n   " 
    } 
    } 
} 
+0

我無法使用getResourceAsStream() - RSS源是外部URL。我用你建議的基本解決方案更新了我的文章。它拋出一個例外。請查看最新更新的帖子。非常感謝你。 –

+0

@PacificNW_Lover您顯示的錯誤與'new URL(「samplefeed.com/feed」)無關。openStream();''關於'jackson-core' /'jackson-dataformat-xml'版本不兼容。如果不知道你的堆棧,很難分辨出發生了什麼。你能顯示你的gradle/maven依賴嗎? – varren

+0

我更新了大多數我的pom.xml文件到2.9.1,現在有一個新問題。請參閱修訂後的文章...現在它給了我一個HTTP 403.你是否知道一個不同的機制來從該外部URL獲取XML作爲字符串? –

1

別人也有問題,將XML轉換到使用傑克遜JSON和只拿到了最後一個元素:Converting xml to json using jackson

所以不是傑克遜,你可以嘗試JSON in Java

<dependency> 
    <groupId>org.json</groupId> 
    <artifactId>json</artifactId> 
    <version>20170516</version> 
</dependency> 

示例代碼

import java.io.*; 
import java.net.*; 
import org.json.JSONObject; 
import org.json.XML; 
... 
public static void main(String[] args) throws Exception { 
    String xmlString = readUrlToString("http://www.feedforall.com/sample.xml"); 
    JSONObject xmlJSONObj = XML.toJSONObject(xmlString); 
    String jsonPrettyPrintString = xmlJSONObj.toString(4); 
    System.out.println(jsonPrettyPrintString); 
} 

public static String readUrlToString(String url) { 
    BufferedReader reader = null; 
    String result = null; 
    try { 
     URL u = new URL(url); 
     HttpURLConnection conn = (HttpURLConnection) u.openConnection(); 
     conn.setRequestProperty("User-Agent", "Mozilla/5.0"); 
     conn.setRequestMethod("GET"); 
     conn.setDoOutput(true); 
     conn.setReadTimeout(2 * 1000); 
     conn.connect(); 
     reader = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
     StringBuilder builder = new StringBuilder(); 
     String line; 
     while ((line = reader.readLine()) != null) { 
      builder.append(line).append("\n"); 
     } 
     result = builder.toString(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     if (reader != null) { 
      try { reader.close(); } catch (IOException ignoreOnClose) { } 
     } 
    } 
    return result; 
}  

另見:Quickest way to convert XML to JSON in Java

+0

謝謝,但對不起,你的代碼給了我一個HTTP 403. –

+0

增加了一行'conn.setRequestProperty(「User-Agent」,「Mozilla/5.0」);'。現在應該在阻止Java作爲用戶代理的網站上工作。 – janih

+1

因爲你的初始代碼對我有幫助(例如,推薦Java庫的JSON並提供maven依賴),我會給你100個獎勵點。 –

0

Why doesn't it display all of entries?

XML在「對象」和「數組」的概念之間沒有區別,因此只打印了單個的item,而不是所有的項目。

這裏是修復,除了依賴你正在使用中,我使用2個額外的罐子,並有如下幾點: -

enter image description here

代碼:

import java.io.IOException; 
import java.io.InputStream; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 

import org.apache.commons.io.IOUtils; 
import org.json.JSONObject; 
import org.json.XML; 

import com.fasterxml.jackson.databind.ObjectMapper; 
import com.fasterxml.jackson.databind.SerializationFeature; 

public class TestClass { 

    public static void main(String[] args) throws Exception { 
     InputStream xml = getInputStreamFromUrlData("http://www.feedforall.com/sample.xml"); 
     String xmlString = IOUtils.toString(xml); 
     JSONObject jsonObject = XML.toJSONObject(xmlString); 
     ObjectMapper objectMapper = new ObjectMapper(); 
     objectMapper.enable(SerializationFeature.INDENT_OUTPUT); 
     Object json = objectMapper.readValue(jsonObject.toString(), Object.class); 
     String output = objectMapper.writeValueAsString(json); 
     System.out.println(output); 
    } 

    public static InputStream getInputStreamFromUrlData(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; 
    } 
} 

樣品執行命令(檢查item是對象的數組,而不是一個單一的對象)

URL: http://www.feedforall.com/sample.xml 
Response Code : 200 
{ 
    "rss" : { 
    "channel" : { 
     "image" : { 
     "link" : "http://www.feedforall.com/industry-solutions.htm", 
     "width" : 48, 
     "description" : "FeedForAll Sample Feed", 
     "title" : "FeedForAll Sample Feed", 
     "url" : "http://www.feedforall.com/ffalogo48x48.gif", 
     "height" : 48 
     }, 
     "copyright" : "Copyright 2004 NotePage, Inc.", 
     "item" : [ { 
     "comments" : "http://www.feedforall.com/forum", 
     "link" : "http://www.feedforall.com/restaurant.htm", 
     "description" : "<b>FeedForAll </b>helps Restaurant's communicate with customers. Let your customers know the latest specials or events.<br>\r\n<br>\r\nRSS feed uses include:<br>\r\n<i><font color=\"#FF0000\">Daily Specials <br>\r\nEntertainment <br>\r\nCalendar of Events </i></font>", 
     "title" : "RSS Solutions for Restaurants", 
     "category" : { 
      "domain" : "www.dmoz.com", 
      "content" : "Computers/Software/Internet/Site Management/Content Management" 
     }, 
     "pubDate" : "Tue, 19 Oct 2004 11:09:11 -0400" 
     }, { 
     "comments" : "http://www.feedforall.com/forum", 
     "link" : "http://www.feedforall.com/schools.htm", 
     "description" : "FeedForAll helps Educational Institutions communicate with students about school wide activities, events, and schedules.<br>\r\n<br>\r\nRSS feed uses include:<br>\r\n<i><font color=\"#0000FF\">Homework Assignments <br>\r\nSchool Cancellations <br>\r\nCalendar of Events <br>\r\nSports Scores <br>\r\nClubs/Organization Meetings <br>\r\nLunches Menus </i></font>", 
     "title" : "RSS Solutions for Schools and Colleges", 
     "category" : { 
      "domain" : "www.dmoz.com", 
      "content" : "Computers/Software/Internet/Site Management/Content Management" 
     }, 
     "pubDate" : "Tue, 19 Oct 2004 11:09:09 -0400" 
     }, { 
     "comments" : "http://www.feedforall.com/forum", 
     "link" : "http://www.feedforall.com/computer-service.htm", 
     "description" : "FeedForAll helps Computer Service Companies communicate with clients about cyber security and related issues. <br>\r\n<br>\r\nUses include:<br>\r\n<i><font color=\"#0000FF\">Cyber Security Alerts <br>\r\nSpecials<br>\r\nJob Postings </i></font>", 
     "title" : "RSS Solutions for Computer Service Companies", 
     "category" : { 
      "domain" : "www.dmoz.com", 
      "content" : "Computers/Software/Internet/Site Management/Content Management" 
     }, 
     "pubDate" : "Tue, 19 Oct 2004 11:09:07 -0400" 
     }, { 
     "comments" : "http://www.feedforall.com/forum", 
     "link" : "http://www.feedforall.com/government.htm", 
     "description" : "FeedForAll helps Governments communicate with the general public about positions on various issues, and keep the community aware of changes in important legislative issues. <b><i><br>\r\n</b></i><br>\r\nRSS uses Include:<br>\r\n<i><font color=\"#00FF00\">Legislative Calendar<br>\r\nVotes<br>\r\nBulletins</i></font>", 
     "title" : "RSS Solutions for Governments", 
     "category" : { 
      "domain" : "www.dmoz.com", 
      "content" : "Computers/Software/Internet/Site Management/Content Management" 
     }, 
     "pubDate" : "Tue, 19 Oct 2004 11:09:05 -0400" 
     }, { 
     "comments" : "http://www.feedforall.com/forum", 
     "link" : "http://www.feedforall.com/politics.htm", 
     "description" : "FeedForAll helps Politicians communicate with the general public about positions on various issues, and keep the community notified of their schedule. <br>\r\n<br>\r\nUses Include:<br>\r\n<i><font color=\"#FF0000\">Blogs<br>\r\nSpeaking Engagements <br>\r\nStatements<br>\r\n </i></font>", 
     "title" : "RSS Solutions for Politicians", 
     "category" : { 
      "domain" : "www.dmoz.com", 
      "content" : "Computers/Software/Internet/Site Management/Content Management" 
     }, 
     "pubDate" : "Tue, 19 Oct 2004 11:09:03 -0400" 
     }, { 
     "comments" : "http://www.feedforall.com/forum", 
     "link" : "http://www.feedforall.com/weather.htm", 
     "description" : "FeedForAll helps Meteorologists communicate with the general public about storm warnings and weather alerts, in specific regions. Using RSS meteorologists are able to quickly disseminate urgent and life threatening weather warnings. <br>\r\n<br>\r\nUses Include:<br>\r\n<i><font color=\"#0000FF\">Weather Alerts<br>\r\nPlotting Storms<br>\r\nSchool Cancellations </i></font>", 
     "title" : "RSS Solutions for Meteorologists", 
     "category" : { 
      "domain" : "www.dmoz.com", 
      "content" : "Computers/Software/Internet/Site Management/Content Management" 
     }, 
     "pubDate" : "Tue, 19 Oct 2004 11:09:01 -0400" 
     }, { 
     "comments" : "http://www.feedforall.com/forum", 
     "link" : "http://www.feedforall.com/real-estate.htm", 
     "description" : "FeedForAll helps Realtors and Real Estate companies communicate with clients informing them of newly available properties, and open house announcements. RSS helps to reach a targeted audience and spread the word in an inexpensive, professional manner. <font color=\"#0000FF\"><br>\r\n</font><br>\r\nFeeds can be used for:<br>\r\n<i><font color=\"#FF0000\">Open House Dates<br>\r\nNew Properties For Sale<br>\r\nMortgage Rates</i></font>", 
     "title" : "RSS Solutions for Realtors & Real Estate Firms", 
     "category" : { 
      "domain" : "www.dmoz.com", 
      "content" : "Computers/Software/Internet/Site Management/Content Management" 
     }, 
     "pubDate" : "Tue, 19 Oct 2004 11:08:59 -0400" 
     }, { 
     "comments" : "http://www.feedforall.com/forum", 
     "link" : "http://www.feedforall.com/banks.htm", 
     "description" : "FeedForAll helps <b>Banks, Credit Unions and Mortgage companies</b> communicate with the general public about rate changes in a prompt and professional manner. <br>\r\n<br>\r\nUses include:<br>\r\n<i><font color=\"#0000FF\">Mortgage Rates<br>\r\nForeign Exchange Rates <br>\r\nBank Rates<br>\r\nSpecials</i></font>", 
     "title" : "RSS Solutions for Banks/Mortgage Companies", 
     "category" : { 
      "domain" : "www.dmoz.com", 
      "content" : "Computers/Software/Internet/Site Management/Content Management" 
     }, 
     "pubDate" : "Tue, 19 Oct 2004 11:08:57 -0400" 
     }, { 
     "comments" : "http://www.feedforall.com/forum", 
     "link" : "http://www.feedforall.com/law-enforcement.htm", 
     "description" : "<b>FeedForAll</b> helps Law Enforcement Professionals communicate with the general public and other agencies in a prompt and efficient manner. Using RSS police are able to quickly disseminate urgent and life threatening information. <br>\r\n<br>\r\nUses include:<br>\r\n<i><font color=\"#0000FF\">Amber Alerts<br>\r\nSex Offender Community Notification <br>\r\nWeather Alerts <br>\r\nScheduling <br>\r\nSecurity Alerts <br>\r\nPolice Report <br>\r\nMeetings</i></font>", 
     "title" : "RSS Solutions for Law Enforcement", 
     "category" : { 
      "domain" : "www.dmoz.com", 
      "content" : "Computers/Software/Internet/Site Management/Content Management" 
     }, 
     "pubDate" : "Tue, 19 Oct 2004 11:08:56 -0400" 
     } ], 
     "lastBuildDate" : "Tue, 19 Oct 2004 13:39:14 -0400", 
     "link" : "http://www.feedforall.com/industry-solutions.htm", 
     "description" : "RSS is a fascinating technology. The uses for RSS are expanding daily. Take a closer look at how various industries are using the benefits of RSS in their businesses.", 
     "generator" : "FeedForAll Beta1 (0.0.1.8)", 
     "language" : "en-us", 
     "title" : "FeedForAll Sample Feed", 
     "managingEditor" : "[email protected]", 
     "pubDate" : "Tue, 19 Oct 2004 13:38:55 -0400", 
     "webMaster" : "[email protected]", 
     "docs" : "http://blogs.law.harvard.edu/tech/rss", 
     "category" : { 
     "domain" : "www.dmoz.com", 
     "content" : "Computers/Software/Internet/Site Management/Content Management" 
     } 
    }, 
    "version" : 2 
    } 
}