2012-03-07 74 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