2012-03-07 87 views
0

我知道有关XML的问题在这里已经回答了数百万次,但是我找不到适合我的任何答案。这是我的问题,欢迎任何帮助!iOS解析整个XML元素

阅读我的服务器从一个PHP脚本一个XML文件,并将其发送到NSXMLParser后,我无法找到一个办法让在XML称为“名称”的所有元素,才能在我的应用程序后以填充UITableView。我的目标是读取整个XML文件,获取名为“name”的所有元素并将它们存储在NSMutableArray中以填充表视图。

因此,这里是通过调用一个PHP脚本传递到NSXMLParser XML代码:

<?xml version="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/xsl" href="auctions_xsl.xsl"?><auctions> 
<auction> 
    <id>1</id> 
    <name>Leilão Tijuca</name> 
    <description>Casa de vila, ótimo estado e ótima localização. Leiloeiro responsável: Fulano de Tal. Contato pelo telefone 3554-5555</description> 
    <date>2012-03-06</date> 
    <imagepath1>imagem 1</imagepath1> 
    <imagepath2>imagem 2</imagepath2> 
    <imagepath3>imagem 3</imagepath3> 
    <location>Rua São Francisco Xavier, 390 - Tijuca - Rio de Janeiro - RJ</title> 
</auction> 
<auction> 
    <id>2</id> 
    <name>Leilão Barra</name> 
    <description>Cobertura ótimo estado. Leiloeiro responsável Leandro. Contato pelo tel: 3554-9356</description> 
    <date>2012-03-28</date> 
    <imagepath1>001</imagepath1> 
    <imagepath2>002</imagepath2> 
    <imagepath3>003</imagepath3> 
    <location>Avenida das Américas, 500 - Barra - Rio de Janeiro - RJ</title> 
</auction> 
<auction> 
    <id>3</id> 
    <name>Leilão Flamengo</name> 
    <description>Apartamento andar baixo. Localizado em frente ao metrô. Leiloeiro Marcel pelo tel 3554-6678</description> 
    <date>2012-03-18</date> 
    <imagepath1>im1</imagepath1> 
    <imagepath2>im2</imagepath2> 
    <imagepath3>im3</imagepath3> 
    <location>Avenida Oswaldo Cruz, 110 - Flamengo - Rio de Janeiro - RJ</title> 
</auction> 

这里是我的Objective-C代码部分:(错件评论)

-(void)viewDidLoad { 

[super viewDidLoad]; 
NSURL *url = [NSURL URLWithString:@"http://leandroprellapps.capnix.com/script.php"]; 
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url]; 
allNames = [NSMutableArray arrayWithObjects: nil]; //declared as instance variable in .h file 
xmlParser.delegate = self; 
[xmlParser parse]; 
NSLog(@"%@",allNames); //getting wrong data here 
} 

//and later on 

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict { 

// SUPPOSE TO READ ALL XML FILE AND FIND ALL "name" ELEMENTS 
if ([elementName isEqualToString:@"name"]) { 

    self.currentName = [[NSMutableString alloc] init];  
    } 
} 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 

[self.currentName appendString:string]; //passing the value of the current elemtn to the string 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { 

if ([elementName isEqualToString:@"name"]) { 

    NSLog(@"%@", currentName); //show current element 
    [allNames addObject:currentName];//not only is not reading all xml "name" elements but also adding another elements not shown in above NSLog....   
    } 
} 

为了举例说明,这里是我的NSLog结果:

2012-03-06 22:51:37.622 Leilao Invest 2.0[10032:f803] Leilão Tijuca 

2012-03-06 22:51:37.623 Leilao Invest 2.0[10032:f803] (
"Leil\U00e3o Tijuca\n\t\tCasa de vila, \U00f3timo estado e \U00f3tima localiza\U00e7\U00e3o. Leiloeiro respons\U00e1vel: Fulano de Tal. Contato pelo telefone 3554-5555\n\t\t2012-03-06\n\t\timagem 1\n\t\timagem 2\n\t\timagem 3\n\t\tRua S\U00e3o Francisco Xavier, 390 - Tijuca - Rio de Janeiro - RJ" 

注意,第一NSLog显示当前元素的正确名称,但在第二NSLog,将当前元素后做我的MutableArray,它只显示所谓的“名”的第一个元素和一堆其他元素不应该被添加,并且它不保留字符串的编码(NSISOLating1)。

+0

我建议您使用Google XML Parser(GDataXMLNode)来解析XML。它具有更多的功能和轻量级。一个解析XML的函数。它也支持xPath。 – Raptor 2012-03-07 02:08:48

+0

我会尝试使用本地XML解析器多一点,如果不成功,我会给谷歌XML解析器尝试。谢了哥们! – lsp 2012-03-07 03:40:12

回答

1

这是因为当解析器发现任何标签之间的东西时,foundCharacters函数会被调用。

在你的情况下,你的第一个标记是'id',因为它不匹配名字,你的currentName没有初始化,因此当字符串currentName为零时,字符不会附加值'2'。然后第二个标签是名称,所以它符合您的条件并初始化字符串。从这一点,标签之间找到的每个字符都会附加到currentName。

如果您只想解析名称,一种方法是在将字符附加到currentName之前检查当前标记是否为'name'。

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict { 

    // SUPPOSE TO READ ALL XML FILE AND FIND ALL "name" ELEMENTS 
    if ([elementName isEqualToString:@"name"]) { 
     isNameTag = YES; 
     self.currentName = [[NSMutableString alloc] init];  
    } 
} 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 
    if (isNameTag) { 
     [self.currentName appendString:string]; //passing the value of the current elemtn to the string 
    } 
} 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { 

    if ([elementName isEqualToString:@"name"]) { 
     isNameTag = NO; // disable the flag 
     NSLog(@"%@", currentName); //show current element 
     [allNames addObject:currentName];//not only is not reading all xml "name" elements but also adding another elements not shown in above NSLog....   
    } 
} 
+0

谢谢队友!它有点工作。现在只有名称被添加到我的数组中,但我仍然无法获取XML中的所有“名称”元素。它只添加第一个元素。在xml中有三个名为“name”的元素。是否有办法让分析器扫描所有的xml文件? – lsp 2012-03-07 03:37:42

+0

我的意思是,isnt解析器函数执行到文件结尾? – lsp 2012-03-07 03:39:10

+0

是的,它解析,直到文档结束。您是否在didEndElement中记录了解析的名称?这是显示全部3个名字还是只显示名字? – Hanon 2012-03-07 04:24:02