2012-03-31 3 views
1

지난 며칠 동안 NSXMLParser를 설정하는 방법을 찾으려고했습니다. 내가 구문 분석해야 할 XML 파일은 다음과 같습니다 : 나는 NSXMLParser의 대리자 메서드 구현 한로컬 XML 파일을 구문 분석하도록 NSXMLParser를 설정하십시오.

<matrix> 
    <row>eraser*met</row> 
    <row>debone*anat</row> 
    <row>ani*jalisco</row> 
    <row>madwoman*on</row> 
    <row>**joy*itsme</row> 
    <row>isao***amad</row> 
    <row>mends*mio**</row> 
    <row>be*parental</row> 
    <row>insipid*hai</row> 
    <row>bail*modern</row> 
    <row>esse*scored</row> 
</matrix> 

:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { 

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { 

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 

을하지만 실제로는 "매트릭스"를 단계별로하는 방법을 알아낼 수 없습니다 행을 배열에 저장하십시오.

도와 주시면 감사하겠습니다.

안부 세바스찬

편집 : 여기

전체 XML 파일입니다 :

<?xml version="1.0" encoding="UTF-8"?> 
<grid version="1"> 
    <matrix> 
     <row>eraser*met*</row> 
     <row>debone*anat</row> 
     <row>ani*jalisco</row> 
     <row>madwoman*on</row> 
     <row>**joy*itsme</row> 
     <row>isao***amad</row> 
     <row>mends*mio**</row> 
     <row>be*parental</row> 
     <row>insipid*hai</row> 
     <row>bail*modern</row> 
     <row>esse*scored</row> 
    </matrix> 
    <clues> 
     <across> 
      <clue>Rubber</clue> 
      <clue>Intro to physics?</clue> 
      <clue>Fish prep?</clue> 
      <clue>Med school subj.</clue> 
      <clue>Tropical cuckoo bird</clue> 
      <clue>State in W Mexico</clue> 
      <clue>Insane female</clue> 
      <clue>Not off</clue> 
      <clue>Happiness</clue> 
      <clue>&quot;Who's there?&quot; response</clue> 
      <clue>Golfer Aoki</clue> 
      <clue>Diary of ___ Housewife</clue> 
      <clue>Fixes</clue> 
      <clue>O Sole ___</clue> 
      <clue>To exist</clue> 
      <clue>Maternal or paternal</clue> 
      <clue>Vapid</clue> 
      <clue>Yes, in Yokohama</clue> 
      <clue>Remove water from a boat</clue> 
      <clue>Contemporary</clue> 
      <clue>&quot;___ quam videri&quot; (North Carolina's motto)</clue> 
      <clue>Tallied</clue> 
     </across> 
     <down> 
      <clue>Dutch cheese</clue> 
      <clue>Drink</clue> 
      <clue>Actress Sofer</clue> 
      <clue>Perceived to be</clue> 
      <clue>Ivory Coast's largest city</clue> 
      <clue>Lisa, to Bart, briefly</clue> 
      <clue>Therefore</clue> 
      <clue>Stack of firewood</clue> 
      <clue>Take pleasure in</clue> 
      <clue>Drain</clue> 
      <clue>500 sheets</clue> 
      <clue>Lens holders</clue> 
      <clue>My ___, Vietnam</clue> 
      <clue>Red Bordeaux</clue> 
      <clue>Preserve</clue> 
      <clue>Perform</clue> 
      <clue>Printing widths</clue> 
      <clue>Suffocate</clue> 
      <clue>Puget Sound city</clue> 
      <clue>Swiss river</clue> 
      <clue>Did penance</clue> 
      <clue>Swedish soprano Jenny</clue> 
     </down> 
    </clues> 
    <hints> 
     <across> 
      <hints></hints> 
      <hints></hints> 
      <hints></hints> 
      <hints></hints> 
      <hints></hints> 
      <hints></hints> 
      <hints></hints> 
     </across> 
     <down> 
      <hints></hints> 
      <hints></hints> 
      <hints></hints> 
      <hints></hints> 
      <hints></hints> 
      <hints></hints> 
      <hints></hints> 
     </down> 
    </hints> 
</grid> 

나는 아직 힌트에 입력하지 않은,하지만 그들이 거기에있을 것입니다 출시 전에.

+0

이 데이터 만 있거나 더 많은 데이터가 XML 파일에 들어 있습니다. pls는 XML 전체 형식을 제공하므로 구문 분석 코드를 제공하므로 nsxml 파서가 더 좋습니다. – parag

+0

안녕하세요. 빠른 응답을 보내 주셔서 감사합니다. 전체 XML 파일을 볼 수 있도록 내 게시물을 편집했습니다. –

+0

전체 XML을 구문 분석 할 수있는 코드를 만들 수 있습니까? :) –

답변

1

당신은 이미 올바른 길을 걷고 있습니다. NSXMLParserparse 메서드를 호출 할 때 이러한 메서드를 호출하기 시작합니다. 귀하의 질문에 약간의 샘플 xml이 있기 때문에 사용자 정의 NSXMLParserDelegate 클래스를 구현하는 방법의 견본 (ruff)이 있습니다. 제발 내 프로젝트 폴더에 "MatrixList.xml"라는 파일에 위의 XML을 복사했습니다.

MatrixList.h :

#import <Foundation/Foundation.h> 
@interface MatrixList : NSObject <NSXMLParserDelegate> 
@property (readonly) NSMutableArray *rows; // property to access results 
-(id)initWithContentsOfURL:(NSURL *)url; 
@end 

MatrixList.m :

#import "MatrixList.h" 
@implementation MatrixList{ 
    NSXMLParser *parser; 
    NSMutableString *charactersFound; 
} 
@synthesize rows = _rows; 
-(void)parserDidStartDocument:(NSXMLParser *)parser{ 
     // These objects are created here so that if a document is not found they will not be created 
    _rows = [[NSMutableArray alloc] init]; 
    charactersFound = [[NSMutableString alloc] init]; 
} 
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ 
    // clear the characters for new element 
    [charactersFound setString:@""]; 
} 
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ 
    // add string found to the mutable string 
    [charactersFound appendString:string]; 
} 
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ 
    if ([elementName isEqualToString:@"row"]){ 
     // If we are done with a row add the rows contents, a string, to the rows array 
     [_rows addObject:[charactersFound copy]]; 
    } 
    [charactersFound setString:@""]; 
} 
-(void)parserDidEndDocument:(NSXMLParser *)parser{ 
    // This method is handy sometimes 
} 
-(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{ 
    NSLog(@"error:%@",parseError.localizedDescription); 
} 
-(id)initWithContentsOfURL:(NSURL *)url{ 
    if ((self = [super init])){ 
     parser = [[NSXMLParser alloc] initWithContentsOfURL:url]; 
     parser.delegate = self; 
     [parser parse]; // This is for an example, You might not want to call parse here, depending on context 
    } 
    return self; 
} 
@end 

이 클래스는과 같이 사용됩니다 : 위의 코드와

// My copy is in the bundle, You could use a url for the docs directory instead 
NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"MatrixList" withExtension:@"xml"]; 
MatrixList *matrix = [[MatrixList alloc] initWithContentsOfURL:fileURL]; 
NSLog(@"rows:%@",matrix.rows); 

콘솔이 생성됩니다

,536,
rows:(
    "eraser*met", 
    "debone*anat", 
    "ani*jalisco", 
    "madwoman*on", 
    "**joy*itsme", 
    "isao***amad", 
    "mends*mio**", 
    "be*parental", 
    "insipid*hai", 
    "bail*modern", 
    "esse*scored" 
) 

이 코드는 전혀 개선되지 않았지만 일부 기본 XML을 구문 분석하는 방법에 대한 좋은 예라고 생각합니다.

+0

고맙습니다 NJones :) 배열에서 모든 단일 문자를 저장하는 것이 가장 빠르고 가장 빠른 방법을 알고 있습니까? :) –

관련 문제