数组NSXML Parser IOS中的数组

时间:2013-01-09 09:39:04

标签: ios nsxmlparser

<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解析:

 - (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台打印机,但是当我试图获得这2个坐标时,我只得到最后一个坐标两次?问题是什么。我哪里错了你能帮助我吗? 提前谢谢..

1 个答案:

答案 0 :(得分: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;
    }