1
我正在使用NSXMLParser解析遠程XML文件以在我的應用程序中顯示信息。/n從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>
appendString不適用於NSString的數據類型。 – NSFeaster
將currentElement的數據類型,NSString更改爲NSMutableString – Hector
感謝您的邏輯!有用! – NSFeaster