2014-12-22 4 views
0

tumblr feed에서 블로그 항목을 추출하는 데 Google Feed API을 사용하고 있습니다.RSS 피드에서 HTML 태그를 제거하는 방법은 무엇입니까?

나는 내용을 당길 수 있었지만, 출력과 같은 HTML 태그와 함께 온다 :

<p>I remember one day asking one of my mentors James if he ever got nervous around people. James replied, “Only when I need something from them.”</p>

코드는 간단하다, 아래 :

<script type="text/javascript" src="https://www.google.com/jsapi"></script> 
<script type="text/javascript"> 
google.load("feeds", "1"); 

function initialize() { 
    var feed = new google.feeds.Feed("http://adriennetran.tumblr.com/rss"); 
    feed.load(function(result) { 

    if (!result.error) { 
     var container = document.getElementById("feed"); 

     for (var i = 0; i < result.feed.entries.length; i++) { 
     var entry = result.feed.entries[i]; 
     window.content = document.createTextNode(entry.content); 
     container.appendChild(content); 
     } 
    } 
    }); 
} 


google.setOnLoadCallback(initialize); 

</script> 

I <으로 시작하는 모든 것을 제거하는 함수를 작성하려고 시도했습니다.

content_array = content.split(" "); 

for (i=0; i < content_array.length; i++){ 
    if ((content_array[i].split(""))[0] == "<"){ 
     content_array.splice(i, 1); 
    } 
} 

content2 = content_array.toString(); 

contentobject이고 string이 아니기 때문에 Uncaught TypeError: undefined is not a function 오류가 발생하여 content.split(" ")으로 전화 할 수 없습니다.

나는 문자열로 변환 시도했다, 그러나 이것은 콘솔

의 출력입니다
typeof(content) 
> "object" 

c2 = content.toString() 
> "[object Text]" 

아무도 RSS에서 검색 요소를 조작하는 방법에 대한 아이디어가 있습니까?

+2

추가 서비스를 받으려면 JSfiddle을 제공 할 수 있습니까? – Mouser

답변

3

가 보자

var regExString = /(<([^>]+)>)/ig; //create reg ex and let it loop (g) 
contentString = content.textContent // get text from node (no longer an object but string. 

contentString = contentString.replace(regExString, "") //find all tags and delete them. 
+0

좋은 생각! 나는 그것을 시도했다. 그러나 content.toString은 단지 결과물을 산출한다. "object" WOW는 내가 작성한 긴 함수보다 훨씬 쉬운 .replace 태그이다. – Aspen

+0

맑음. typeof (내용) "개체" c2 = content.toString() "[대상 텍스트]" " – Aspen

+0

@Aspen 내 코드가 업데이트되었습니다. 개체가 텍스트 노드 인 것 같습니다. node.textContent는 마법을 사용해야합니다. – Mouser

0

당신이 jQuery를 당신의 페이지에 포함 된 경우, 당신은 HTML 피드로부터받은 사용하여 노드를 생성하고 그래서 같이 HTML에서 텍스트를 얻을 수 있습니다 :

var html = '<p>I remember one day asking one of my mentors James if he ever got nervous around people. James replied, “Only when I need something from them.”</p>'; 
var text = $(html).text(); // This gets the text from any HTML code and leaves out the tags 
관련 문제