我知道有关XML的问题在这里已经回答了数百万次,但我找不到任何适合我的答案。这是我的问题,欢迎任何帮助!
从我的服务器中的php脚本读取XML文件并将其发送到NSXMLParser
后,我无法找到一种方法来获取该XML中名为“name”的所有元素,以便填充{{1}稍后在我的应用程序中。我的目标是读取整个XML文件,获取名为“name”的所有元素,并将它们存储在UITableView
中以填充表视图。
所以这是通过调用PHP脚本传递给NSMutableArray
的XML代码:
NSXMLParser
这是我的Objective-c代码的一部分:(错误的部分评论)
<?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>
举个例子,这是我的NSLog结果:
-(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....
}
}
)
请注意,第一个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
中,在添加当前元素后执行我的MutableArray,它只显示名为“name”的第一个元素,一堆其他不应该添加的元素,并没有保留字符串的编码(NSISOLating1)。
答案 0 :(得分:1)
这是因为当解析器在任何标记之间找到某些东西时,将调用foundCharacters函数。
在您的情况下,您的第一个标记是'id',因为它与名称不匹配,您的currentName尚未初始化,因此foundCharacters不会附加值'2',因为此时currentName为nil。然后第二个标签是名称,因此它符合您的条件并初始化字符串。从这一点开始,标记之间找到的每个字符都将附加到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....
}
}