2016-06-30 2 views
1

이 사이트에서 콘텐츠를 찾을 수있는 xml 파일을 구문 분석해야합니다 : http://jpg.tartu.ee/tunniplaan/xml/arvestus1.xml (이 파일을 보려면 페이지 소스 코드를 봐야합니다. (파일이 너무 커서 게시 할 수 없습니다. 여기)NSXMLParser가 모든 요소를 ​​읽지 않습니다

내가 먼저이 파일을 다음 내 응용 프로그램에서 데이터를 읽고 다운로드합니다.

을 내가 NSXMLParser을 사용하고 있습니다. 그 파일에서 내가 요소 TimeTableSchedule 속성이 필요하지만, 파일에서 해당 요소를 NSXMLParser 찾을 수없는 이상, 하지만 존재하지 않습니다.

요소를 찾았는지 확인했습니다. 코드 아래에 TimeTableSchedule이라는 이름이 붙었지만 그렇지 않습니다! 이 요소 TimeTableSchedule를 찾을 수없는 이유 : 그것은 "TimeTableSchedule"

func parser(parser: NSXMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) { 
     print(elementName) 
    } 

내 질문

은 제외하고 다른 모든 요소를 ​​출력한다? TimeTableSchedule 요소에는 Period이라는 두 가지 특성이 있습니다. TimeTableSchedule 요소 속성에 액세스 할 수있는 방법이 있습니까? NSXMLParser

답변

1

예, XML 형식이 올바르지 않습니다. 다음과 같은 내용이 포함됩니다.

<TimeTableSchedule DayID="" Period="" Period="-1" SchoolRoomID="" SubjectGradeID="*28" ClassID="*11" OptionalClassID="" TeacherID=""/> 

Period 속성은 해당 요소에 두 번 나타납니다. 실제로 NSXMLParserDelegate 메서드 parseErrorOccurred을 구현 한 경우 해당 오류가 발생했을 것입니다. 또는 명령 행 프로그램 xmllint을 사용하여 XML을 점검 할 수 있습니다. 당신은 웹 서비스에 XML을 해결할 수없는 점을 감안


, 당신은 이론적으로 클라이언트에서 직접 해결할 수 : NSXMLParser를 사용하거나 해당 요소에 액세스 할 수

let url = NSURL(string: "http://jpg.tartu.ee/tunniplaan/xml/arvestus1.xml")! 
let task = NSURLSession.sharedSession().dataTaskWithURL(url) { data, response, error in 
    guard let data = data where error == nil else { 
     print(error) 
     return 
    } 

    // replace occurrences of `Period="" Period` with `Period` 

    let mutableData = data.mutableCopy() as! NSMutableData 
    let searchData = "Period=\"\" Period".dataUsingEncoding(NSUTF8StringEncoding)! 
    let replacementData = "Period".dataUsingEncoding(NSUTF8StringEncoding)! 

    var range = mutableData.rangeOfData(searchData, options: [], range: NSRange(location: 0, length: mutableData.length)) 
    while range.location != NSNotFound { 
     mutableData.replaceBytesInRange(range, withBytes: replacementData.bytes, length: replacementData.length) 
     range = mutableData.rangeOfData(searchData, options: [], range: NSRange(location: range.location, length: mutableData.length - range.location)) 
    } 

    // now parse 

    let parser = NSXMLParser(data: mutableData) 
    parser.delegate = self 
    parser.parse() 

    // do whatever you want with the parsed data here 
} 
task.resume() 
+0

흠, 좋아,하지만 다른 방법을 다른 xml 파서를 사용해야합니까? –

+0

XML을 수정하거나 다른 파서를 시도해야합니다. – Rob

+0

소스 xml 파일을 변경할 수 없지만 어쨌든 고마워요. –

관련 문제