我知道有关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