레이블이 xml 파싱인 게시물을 표시합니다. 모든 게시물 표시
레이블이 xml 파싱인 게시물을 표시합니다. 모든 게시물 표시

2025년 3월 13일 목요일

구글 트랜드 rss 파싱하기

 

1️⃣ 패키지 설치


npm install axios fast-xml-parser

#rss-parser도 사용해 봤지만 rss-parser는 네임스페이스를 파싱하지 못한다고 한다.

2️⃣ Google Trends RSS 파싱 코드 (google-trends-parser.js)


const axios = require("axios"); const { XMLParser } = require("fast-xml-parser"); const GOOGLE_TRENDS_RSS_URL = "https://trends.google.com/trending/rss?geo=US"; async function fetchGoogleTrends() { try { // RSS 피드 가져오기 const response = await axios.get(GOOGLE_TRENDS_RSS_URL); // XML 파싱 const parser = new XMLParser({ ignoreAttributes: false, parseTagValue: true }); const feed = parser.parse(response.data); // RSS 구조 확인 const items = feed.rss.channel.item; if (!items) { console.error("❌ No items found in the RSS feed."); return; } // 트렌드 뉴스 출력 items.forEach((item, index) => { console.log(`📌 Trend ${index + 1}: ${item.title}`); console.log(` 📰 Link: ${item.link}`); console.log(` 🖼️ Image: ${item["ht:picture"]}`); console.log(` 📰 Source: ${item["ht:picture_source"]}`); // ht:news_item 처리 if (item["ht:news_item"]) { const newsItems = Array.isArray(item["ht:news_item"]) ? item["ht:news_item"] : [item["ht:news_item"]]; console.log(` 🗞️ News Articles:`); newsItems.forEach((news, idx) => { console.log(` ${idx + 1}. ${news["ht:news_item_title"]}`); console.log(` 🔗 ${news["ht:news_item_url"]}`); console.log(` 🖼️ ${news["ht:news_item_picture"]}`); console.log(` 📰 Source: ${news["ht:news_item_source"]}\n`); }); } console.log("------------------------------------------------------"); }); } catch (error) { console.error("❌ Error fetching Google Trends:", error.message); } } fetchGoogleTrends();

2016년 4월 25일 월요일

xml 데이터로 html 뷰 만들기

1. 컨트롤러에서 json으로 변환 후 javascript로 html 구현
<server>
 URL url = new URL("http://www.ut.ac.kr/xmlout/Area.xml"); InputStream is = url.openStream(); String str = ""; StringBuilder sb = new StringBuilder();BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8")); try {
     while ((str = br.readLine())!= null) {
         sb.append(str);     }

 } catch (Exception e) {
     e.printStackTrace(); } finally {
     if (is != null) {
         is.close();         br.close();     }
 }
 JSONObject xmlJSONObj = XML.toJSONObject(sb.toString());

<client>
<script>
var json
function getDoc() {
    json = JSON.parse('${xml}');    var items = json.Info.items;    items.forEach(function (row) {        var cat = row.Category;        var name = row.Name;        var tel = row.Tel;        var lati = row.Latitude;        var longti = row.Longitude;
    });}

</script>

2.jstl로 파싱

<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
<x:parse var="out" xml="${xmlData}"/>
<x:forEach select="$out/Info/items" var="item">
        <li class="map" title="<x:out select="Name"/>" lat="<x:out select="Latitude"/>" lng="<x:out select="Longitude"/>">
[<x:out select="Category"/> ]  상호 : <x:out select="Name"/></li>
</x:forEach>