如何使用xmlParser

时间:2015-08-17 12:05:37

标签: ios xml xml-parsing nsxmlparser

我是iOS开发的新手。我想解析我的xml web服务数组。我的xml响应就像,

 <BookList>
     <Book>
           <BookID>int</BookID>
           <BookName>string</BookName>
           <Pages>string</Pages>
           <Price>string</Price>
     </Book>
     <Book>
           <BookID>int</BookID>
           <BookName>string</BookName>
           <Pages>string</Pages>
           <Price>string</Price>
     </Book>
 </BookList>

以下是我用来解析响应的代码,

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSString *theXML = [[NSString alloc] initWithBytes:
                        [self.webResponseData mutableBytes] length:[self.webResponseData length] encoding:NSUTF8StringEncoding];

    //now parsing the xml
    NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData: self.webResponseData];
    xmlParser.delegate = self;

    // Run the parser
    @try{
        BOOL parsingResult = [xmlParser parse];
    }
    @catch (NSException* exception)
    {
        UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"Server Error" message:[exception reason] delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [alert show];
        return;
    }
}

//Implement the NSXmlParserDelegate methods
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:
(NSString *)qName attributes:(NSDictionary *)attributeDict
{
     currentElement = elementName;
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    // NSLog(@"String : %@", string);
    if ([currentElement isEqualToString:@"BookID"]) {
        NSLog(@"BookID : %@", string);
    }else if ([currentElement isEqualToString:@"BookName"]) {
        NSLog(@"BookName : %@", string);
    }else if ([currentElement isEqualToString:@"Pages"]) {
        NSLog(@"Pages : %@", string);
    }else if ([currentElement isEqualToString:@"Price"]) {
        NSLog(@"Price : %@", string);
    }

}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
   // NSLog(@"Current Element: %@ ",currentElement);
}

使用此代码我可以查看结果,但我可以获得书籍数量。

1 个答案:

答案 0 :(得分:1)

您可以将其添加到.h文件

NSMutableDictionary *dictBook;
NSMutableArray *arrBook;
NSMutableString *elementValue;

现在使用以下代码获取总图书数。

//Implement the NSXmlParserDelegate methods
-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:
(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    currentElement = elementName;
    if ([currentElement isEqualToString:@"BookList"]) {
        arrBook = [[NSMutableArray alloc] init];
    }
    else if ([currentElement isEqualToString:@"Book"]) {
        dictBook = [[NSMutableDictionary alloc] init];
    }
}


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if(!elementValue)
        elementValue = [[NSMutableString alloc] initWithString:string];
    else
      elementValue = string;  //[elementValue appendString:string];

    NSLog(@"elementValue : %@",elementValue);
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    // NSLog(@"Current Element: %@ ",currentElement);

    if ([elementName isEqualToString:@"BookID"] ||
        [elementName isEqualToString:@"BookName"] ||
        [elementName isEqualToString:@"Pages"] ||
        [elementName isEqualToString:@"Price"])
    {
        [dictBook setObject:elementValue forKey:elementName];
    }
    else if ([elementName isEqualToString:@"Book"])
    {
        [arrBook addObject:dictBook];
    }

   // [elementValue setString:@""];
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {
     NSLog(@"Arr Book Count : %lu", (unsigned long)arrBook.count);

}

希望这会对你有所帮助。