Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

iphone - how to parse simple xml

    <NewDataSet>
      <Table>
        <CaseId>743</CaseId>
        <PartyId>11100550</PartyId>
        <CartId>18</CartId>

      </Table>
      <Table>
        <CaseId>742</CaseId>
        <PartyId>11100549</PartyId>
        <CartId>1148</CartId>
        <BusinessID>19</BusinessID>

      </Table>
    </NewDataSet>


        NSData* data = [xmlResponseData dataUsingEncoding:NSUTF8StringEncoding];

    //   NSXMLParser *XMLparser = [[NSXMLParser alloc] initWithData:data];
    //    [XMLparser setDelegate:self];

        BOOL success;

        if (XMLparser) // addressParser is an NSXMLParser instance variable
            [XMLparser release];

        XMLparser = [[NSXMLParser alloc] initWithData:data];
        [XMLparser setDelegate:self];
        [XMLparser setShouldResolveExternalEntities:YES];

        success = [XMLparser parse];   // return value not used
        // if not successful, delegate is informed of error 

       if(success)
            NSLog(@"Sucess Parsed");
        else
            NSLog(@"Error Error Error!!!");

    //  NSLog(@"Parsed string : %@",valueForItem);

    }

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

        if([elementName isEqualToString:@"NewDataSet"]) {
            caseID_List = [[NSMutableArray alloc]init];
        }
        NSLog(@"Processing Element: %@", elementName);

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

        }

    }

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

        if(!currentElementValue) 
            currentElementValue = [[NSMutableString alloc] initWithString:string];
        else
            [currentElementValue appendString:string];

        NSLog(@"Processing Value: %@", currentElementValue);


    }

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

      if([elementName isEqualToString:@"NewDataSet"]) 
return;
    }

I need only CaseId to store from the given xml. Can any one advice me on this simple parsing!

For me its looping! and getting all details

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use the Below Code.Where Table is NSObject Class with the CaseId,PartyId,CartId as a properties in this class.If you have the xml url just called loadXMLByURL method with URl.After parsing you will get Each Object in TableArray which have the Table object with above properties.

NSMutableString *currentNodeContent;
NSXMLParser     *parser;
Tweet           *currentTweet;
bool            isStatus;

-(id) loadXMLByURL:(NSString *)urlString
{
    _tweets         = [[NSMutableArray alloc] init];
    NSURL *url      = [NSURL URLWithString:urlString];
    NSData  *data   = [[NSData alloc] initWithContentsOfURL:url];
    parser          = [[NSXMLParser alloc] initWithData:data];
    parser.delegate = self;
    [parser parse];
    return self;
}

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    currentNodeContent = (NSMutableString *) [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

    - (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
    {
        if ([elementname isEqualToString:@"Table"]) 
        {
            currentTable = [Table alloc];
                isStatus = YES;
        }
    }

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if (isStatus) 
    {
        if ([elementname isEqualToString:@"CaseId"]) 
        {
            currentTable.CaseId = currentNodeContent;
        }
        if ([elementname isEqualToString:@"PartyId"]) 
        {
            currentTable.PartyId = currentNodeContent;
        }
        if ([elementname isEqualToString:@"CartId"]) 
        {
            currentTable.CartId = currentNodeContent;
        }

    }
    if ([elementname isEqualToString:@"Table"]) 
    {
        [self.tableArray addObject:currentTable];
        currentTable = nil;
        currentNodeContent = nil;
    }
}

Let me know if you have any doubt.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...