2013-10-10 5 views
0

나는 내가 XML을 구문 분석 할 응용 프로그램 및 을 개발하지만,하고는 u는 nsxmlparser구문 분석 다중 속성 값은

<?xml version="1.0" encoding='utf-8' ?> 
<Responses Id='7465'> 
<Response RequestID="101" FunctionStatus="0" Message="OK"/> 

<Result> 

<PrimaryID> 
<Item ID="1" Text="Aadhar Card"/> 
<Item ID="2" Text="Voters ID"/> 
<Item ID="3" Text="Driving Licence"/> 
<Item ID="4" Text="PAN Card"/> 
<Item ID="5" Text="Passport"/> 
</PrimaryID> 

<Sex> 
<Item ID="1" Text="Male"/> 
<Item ID="2" Text="Female"/> 
</Sex> 

<Title> 
<Item ID="1" Text="Mr"/> 
<Item ID="2" Text="Ms"/> 
<Item ID="3" Text="Mrs"/> 
</Title> 
</Result> 

</Responses> 
+1

은 당신이 뭔가를 시도하거나 그것을 수행하는 방법 궁금 적이 있습니까? – Alladinian

+0

그래, 시도했지만 단일 태그를 누른 다음 확인 – user2760528

+0

그럼'파서의 attributeDict' : didStartElement : namespaceURI : qualifiedName : attributes :'delegate 메소드가 당신이 찾고있는 것입니다. ([docs here here] (https://developer.apple.com/library/ios/documentation/cocoa/reference/NSXMLParserDelegate_Protocol/Reference/Reference.html#//apple_ref/doc/uid/TP40008632-CH1-SW5)) ...) ... – Alladinian

답변

0

를 사용하여 작업을 수행하는 방법을 다음 여러 속성 의 방법으로 올 때 을 XMLReader를 사용하면 다음과 같이 사전에 그것을 얻을 수 있습니다

file.xml을 리소스 번들에 :

- (void)dataFromXML 
{ 
    // Error 
    NSError *error = nil; 

    // Request 
    NSMutableDictionary *xmlDic = nil; 
    NSData *xmlData = nil; 

    // Get XML 
    xmlData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"file" ofType:@"xml"]]; 

    // Parse the XML Data into a NSDictionary 
    xmlDic = [NSMutableDictionary dictionaryWithDictionary:[XMLReader dictionaryForXMLData:xmlData error:&error]]; 

    // Configure Dictionary 
    xmlDic = [[[xmlDic objectForKey:@"Responses"] objectForKey:@"Response"] objectForKey:@"Result"]; 
    NSLog(@"%@", xmlDic); 
} 

file.xml :

- (void)dataWSFromXML 
{ 
    // Error 
    NSError *error = nil; 

    // Request 
    NSURLRequest *request = nil; 
    NSURLResponse *response = nil; 
    NSMutableDictionary *xmlDic = nil; 
    NSData *xmlData = nil; 

    // Get XML 
    request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://yourXMLfile.xml"]]; 
    xmlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; // You can improve this request 

    // Parse the XML Data into a NSDictionary 
    xmlDic = [NSMutableDictionary dictionaryWithDictionary:[XMLReader dictionaryForXMLData:xmlData error:&error]]; 

    // Configure Dictionary 
    xmlDic = [[[xmlDic objectForKey:@"Responses"] objectForKey:@"Response"] objectForKey:@"Result"]; 
    NSLog(@"%@", xmlDic); 
} 

하지만이처럼 XML 변경 :

<?xml version="1.0" encoding="UTF-8"?> 
<Responses Id='7465'> 
    <Response RequestID="101" FunctionStatus="0" Message="OK"> 
     <Result> 

      <PrimaryID> 
       <Item ID="1" Text="Aadhar Card"/> 
       <Item ID="2" Text="Voters ID"/> 
       <Item ID="3" Text="Driving Licence"/> 
       <Item ID="4" Text="PAN Card"/> 
       <Item ID="5" Text="Passport"/> 
      </PrimaryID> 

      <Sex> 
       <Item ID="1" Text="Male"/> 
       <Item ID="2" Text="Female"/> 
      </Sex> 

      <Title> 
       <Item ID="1" Text="Mr"/> 
       <Item ID="2" Text="Ms"/> 
       <Item ID="3" Text="Mrs"/> 
      </Title> 
     </Result> 
    </Response> 
</Responses>