2013-01-09 77 views
0
<events> 
     <status>OK</status> 
     <name>20121203</name> 
     <descripcion></descripcion> 
     <url_IOS></url_IOS> 

     <printers> 
      <coordenadas_LAT>40.44702054203392</coordenadas_LAT> 
      <coordenadas_LON>-3.703991174697876</coordenadas_LON> 
     </printers> 

     <printers> 
      <coordenadas_LAT>40.448416693004596</coordenadas_LAT> 
      <coordenadas_LON>-3.7036800384521484</coordenadas_LON> 
     </printers> 

    </events> 

==== 大家好, 我有這樣的XML和Im解析這樣的:陣列陣列NSXML分析器IOS

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict 
    { 
     // the start of a 'booth' element is been encountered 
     if([elementName isEqualToString:@"events"]) 
     { 
      self.tmpEvent = [[PublicPrivateEvent alloc] init]; 
     } 

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

     currentElementValue = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
    } 
    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
    { 
     if ([elementName isEqualToString:@"coordenadas_LAT"]) { 

       tmpEvent.latitude =currentElementValue; 
     } 
     if([elementName isEqualToString:@"coordenadas_LON"]) 
     { 
      tmpEvent.longtitude = currentElementValue ; 

     } 
     if([elementName isEqualToString:@"description"]) 
     { 
      tmpEvent.description = currentElementValue; 

     } 

     if([elementName isEqualToString:@"url_IOS"]) 
     { 
      tmpEvent.imgurlios6 = currentElementValue; 

     } 

     if([elementName isEqualToString:@"printers"]) 
     { 

      [printers addObject:tmpEvent]; 
      currentElementValue = nil; 
     } 
     if([elementName isEqualToString:@"status"]) 
     { 
      [events addObject:tmpEvent]; 
      currentElementValue = nil; 
     } 

    } 

=== 我有2臺打印機,但是當IM試圖獲得這2個座標,但即時通訊只獲得最後一個座標兩次?問題是什麼。我在哪裏錯了你能幫我嗎? 在此先感謝..

+0

你能找到答案嗎?如果是的話,你也可以告訴我。一直在尋找一種解析XML的方法,並顯示它們的子元素值名稱。 –

回答

0

是的。我曾是。下面是我的解決方案。希望有幫助。

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

    // the start of a 'booth' element is been encountered 
    if([elementName isEqualToString:@"evento"]) 
    { 
     self.tmpEvent = [[PublicPrivateEvent alloc] init]; 
     self.dictionaryPrinters = [[NSMutableDictionary alloc] init]; 

    } 

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

    currentElementValue = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
} 
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{ 
    if ([elementName isEqualToString:@"coordenadas_LAT"]) { 


     [self.dictionaryPrinters setValue:currentElementValue forKey:@"coordenadas_LAT"]; 
     tmpEvent.latitude =currentElementValue; 


    } 
    if([elementName isEqualToString:@"coordenadas_LON"]) 
    { 
     [self.dictionaryPrinters setValue:currentElementValue forKey:@"coordenadas_LON"]; 
     tmpEvent.longtitude = currentElementValue ; 


    } 
    if([elementName isEqualToString:@"description"]) 
    { 
     tmpEvent.description = currentElementValue; 

    } 
    if([elementName isEqualToString:@"url_imagen_evento_LDPI"]) 
    { 
     tmpEvent.imgurlldp = currentElementValue; 

    } 
    if([elementName isEqualToString:@"nombre"]) 
    { 
     tmpEvent.name = currentElementValue; 

    } 
    if([elementName isEqualToString:@"url_imagen_evento_MDPI"]) 
    { 
     tmpEvent.imgurlmdp = currentElementValue; 

    } 

    if([elementName isEqualToString:@"url_imagen_evento_HDPI"]) 
    { 
     tmpEvent.imgurlhdp = currentElementValue; 

    } 
    if([elementName isEqualToString:@"url_imagen_evento_XHDPI"]) 
    { 
     tmpEvent.imgurlxhdp = currentElementValue; 

    } 
    if([elementName isEqualToString:@"url_imagen_evento_IOS5"]) 
    { 
     tmpEvent.imgurlios5 = currentElementValue; 

    } 
    if([elementName isEqualToString:@"url_imagen_evento_IOS6"]) 
    { 
     tmpEvent.imgurlios6 = currentElementValue; 
    } 

    if([elementName isEqualToString:@"impresora"]) 
    { 
     [printers addObject:[self.dictionaryPrinters copy]]; 

    } 
    if([elementName isEqualToString:@"status"]) 
    { 
     [events addObject:tmpEvent]; 
     currentElementValue = nil; 
    }