XML에 대한 질문에 백만 번 답변을 받았지만 나에게 맞는 답변을 찾을 수 없다는 것을 알고 있습니다. 여기 내 문제가 있으며 어떤 도움을 환영합니다! 전체 XML 요소를 구문 분석하는 iOS
NSXMLParser
에 내 서버에 PHP 스크립트에서 XML 파일을 읽고 그것을 보내면 나중에 내 응용 프로그램의
UITableView
를 채우기 위해 그 XML에서 "이름"이라는 모든 요소를 얻을 수있는 방법을 찾을 수 없습니다. 내 목표는 전체 XML 파일을 읽고 "이름"이라는 모든 요소를 가져 와서
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>
을 그리고 여기 내 목표 - 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 파서 (GDataXMLNode)를 사용하여 XML을 구문 분석하는 것이 좋습니다. 더 많은 기능과 가벼움을 가지고 있습니다. XML을 파싱하는 하나의 함수. xPath도 지원합니다. – Raptor
네이티브 xml 파서를 조금 더 사용 해보겠습니다. suceeded가 아니라면 google xml parser에 시도해 보겠습니다. 고마워! – lsp