iphone -/n 在来自 XML 解析的数组中

标签 iphone ios ios5 xml-parsing

我正在使用 NSXMLParser 解析远程 XML 文件以在我的应用程序中显示信息。

我将字符串存储在 NSMutableArray 中。该信息在 4 个标签中提供,这些标签会重复自身并提供更多信息。

现在,我以“/n”“”的形式在索引处获取对象。 "真实数据", "/n", "真实数据"

我尝试过使用 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>

最佳答案

试试这个:

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
string =[string stringByReplacingOccurrencesOfString:@"\t" withString:@""];
string =[string stringByReplacingOccurrencesOfString:@"\n" withString:@""];
if(!currentElement) 
    currentElement = [[NSMutableString alloc] initWithString:string];
else
    [currentElement appendString:string];

关于iphone -/n 在来自 XML 解析的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10591961/

相关文章:

iphone - 继续向 iPhone 中的导航 Controller 添加新 View 吗?

iPhone - 这个屏幕是否可用和可定制,还是我需要从头开始创建它?

ios - ViewController 容器 UIView 的大小

ios - 运行有限长度的后台任务

ios - Xcode 5/iOS7 : Trying to add constraints to resize between iPhone 3. 5"and 4"屏幕

ios - 当我的 ViewController 被导航时,AFURLSessionManager 应用程序崩溃了

ios - 聚焦时如何修复 UISearchController 搜索栏调整大小?

ios - 无法使用类型为 'animateWithDuration' 的参数列表调用 '(...)'

iOS5:模式 UIViewController 播放 Youtube 视频时 UIWebView 出现异常

ios - 在 IOS 5 上将应用程序限制为特定方向