NSXMLParserを使用してリモートXMLファイルを解析し、アプリケーションに情報を表示しています。
文字列をNSMutableArrayに保存しています。情報は4つのタグで提供され、より多くの情報が繰り返されます。
現在、 "/n"""のような形式のインデックスでオブジェクトを取得しています。「realdata」、「/ n」、「realdata」
Stringsbyreplacingoccurencesofstring関数を使用してみましたが、/ nが削除され、空のインデックスが返されるようです。
助けてください。
コード:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
currentElement = elementName ;
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
string1 = string;
string1 = [string1 stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@" \n\t"]];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
if([currentElement isEqualToString:@"CourseID"])
{
[CourseID addObject:string1];
}
if([currentElement isEqualToString:@"CourseName"])
{
[CourseName addObject:string1];
}
if ([currentElement isEqualToString:@"CourseDescription"])
{
[CourseDescription addObject:string1];
}
if ([currentElement isEqualToString:@"DescriptionType"])
{
[DescriptionType addObject:string1];
}
}
- (void)parserDidEndDocument:(NSXMLParser *)parser{
NSLog(@"Course Name: %@",CourseName);
NSLog(@"CourseID: %@",CourseID);
NSLog(@"CourseDescription: %@",CourseDescription);
NSLog(@"DescriptionType: %@",DescriptionType);
}
出力:
2012-05-15 04:03:09.011 DispatchTestPrep[2290:f803] Course Name: (
"Weather Theory",
Turbulence,
"Non-Graphic Weather",
"Graphic Weather",
Airport
)
2012-05-15 04:03:09.012 DispatchTestPrep[2290:f803] CourseID: (
1,
2,
3,
4,
5
)
2012-05-15 04:03:09.012 DispatchTestPrep[2290:f803] CourseDescription: (
"Aviation Weather Theory",
Turbulence,
"Non-Graphic Weather",
"Graphic Weather",
Airport
)
2012-05-15 04:03:09.012 DispatchTestPrep[2290:f803] DescriptionType: (
Text,
"",
Text,
"",
Text,
"",
Text,
"",
Text,
"",
""
)
ご覧のとおり、最後の「DescriptionType」配列は、インデックスにあるこれらの空のオブジェクトを示しています。
解析されるXMLファイルは次のとおりです。
<NewDataSet><Table><CourseID>1</CourseID><CourseName>Weather Theory</CourseName><CourseDescription>Aviation Weather Theory</CourseDescription><DescriptionType>Text</DescriptionType></Table><Table><CourseID>2</CourseID><CourseName>Turbulence</CourseName><CourseDescription>Turbulence</CourseDescription><DescriptionType>Text</DescriptionType></Table><Table><CourseID>3</CourseID><CourseName>Non-Graphic Weather</CourseName><CourseDescription>Non-Graphic Weather</CourseDescription><DescriptionType>Text</DescriptionType></Table><Table><CourseID>4</CourseID><CourseName>Graphic Weather</CourseName><CourseDescription>Graphic Weather</CourseDescription><DescriptionType>Text</DescriptionType></Table><Table><CourseID>5</CourseID><CourseName>Airspace & Airport</CourseName><CourseDescription>Airspace & Airport</CourseDescription><DescriptionType>Text</DescriptionType></Table></NewDataSet>