我知道有關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)。
我建議您使用Google XML Parser(GDataXMLNode)來解析XML。它具有更多的功能和輕量級。一個解析XML的函數。它也支持xPath。 – Raptor 2012-03-07 02:08:48
我會嘗試使用本地XML解析器多一點,如果不成功,我會給谷歌XML解析器嘗試。謝了哥們! – lsp 2012-03-07 03:40:12