2016-07-03 1 views
0

RSS 피드에서 인클로저 태그의 URL 내용을 추출하는 데 어려움이 있습니다. 임 엑스 코드 7 스위프트 2.SWIFT 2를 사용하여 인클로저 태그 rss xml에서 URL 값을 추출 할 수 없습니다

내 NSXMLDelegate 구문 분석 코드를 개발하는 것은 다음과 같습니다 :

func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) { 
    eName = elementName 
    if elementName == "item" { 
     postTitle = String() 
     postLink = String() 
     postDate = String() 
     postImageUrl = String() 
    } 
} 

아래 관련 소스 코드로 위의 RSS XML 피드

func parser(parser: NSXMLParser, foundCharacters string: String) { 
    let data = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) 
    if (!data.isEmpty) { 
     print("eName Start") 
     print(eName) 
     print(data,"DATAAAA") 
     print("eName END") 
     if eName == "title" { 
      postTitle += data//TEST OK 
     } else if eName == "link" { 
      postLink += data//TEST OK 
     } else if eName == "pubDate" { 

      postDate += data//TEST OK 
     } 
     else if eName == "enclosure"{ 
      print(data)//THIS IS EMPTY WHY !? 
      print("enc details") 
      postImageUrl += data 
     } 
    } 
} 

을 구문 분석에 대한 책임, RSS 피드에서 "제목", "pubDate"및 "링크"태그를 추출 할 수 있었지만 인클로저 태그는 추출 할 수 없었습니다. "인클로저"감지시 데이터를 덤프하려고하면 반환 된 데이터가 비어 있습니다.

RSS 개체를 올바르게 필터링하지 않는이 코드 줄에 문제가 있다고 생각됩니다.

let data = string.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) 

RSS 피드의 소스는 다음과 같습니다 www.securitymagazine.com/rss/15

아무도 여기에 몇 가지 통찰력을 제공 할 수주십시오? 나는 그것을 매우 고맙게 생각한다!

+0

내가이 코드를 RSS 객체의 필터링을 교체하려고 시도 : 그래서, 당신은 다음과 같은 방법을 수정해야 데이터를하자 = string.stringByTrimmingCharactersInSet (NSCharacterSet.whitespaceAndNewlineCharacterSet())과는 할 수 없습니다 인클로저 태그도 감지하십시오 –

+0

ename에 대한 질문에 대한 대답을 반영하여 제 질문을 편집했습니다. RSS XML 피드에서 찾은 요소가 포함 된 문자열입니다. 이렇게하면 코드가 XML 내의 각 항목을 추적 할 수 있습니다. –

답변

2

은 RSS에서 "케이스"요소는 다음과 같습니다

<enclosure url="http://www.securitymagazine.com/ext/resources/SEC/2016/0616/Boschdata.jpg?1467317413" type="image/jpeg" length="280480"/> 

URL은 소자의 특성이 아닌 시작 태그 다음 문자 (하는 #text) 데이터를 포함한다. parser(_:didStartElement:...) 메소드의 요소 속성에 액세스 할 수 있습니다.

func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) { 
    eName = elementName 
    switch elementName { 
    case "item": 
     postTitle = String() 
     postLink = String() 
     postDate = String() 
     postImageUrl = String() 
    case "enclosure": 
     if let urlString = attributeDict["url"] { 
      //...use `urlString` appropriately 
      print(urlString) 
      print("enc details") 
      postImageUrl += urlString 
     } else { 
      print("malformed element: enclosure without url attribute") 
     } 
    //...cases for other element name 
    default: 
     //... 
     break 
    } 
} 
관련 문제