简体   繁体   中英

How to add NSXML parser in class?

I want to add NSXML parser in to my Objective-C class and parse XML file

in .h file

NSMutableData *myWebData; NSXMLParser *myXMLParser; NSString *tempStr;

in .m life

    (void)ViewDidLoad
    {
    NSString *soapMsg=@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
        "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"
        "<soap:Body>\n"
        "<GetCountryList xmlns=\"http://tempuri.org/\" />"
        "</soap:Body>"
        "</soap:Envelope>";

        NSURL *myurl=[NSURL URLWithString:@"http://iphone.dotnetdemosite.com/Health4Life/Health4Life_Service.asmx?op=GetCountryList"];
        NSMutableURLRequest *connectionReq=[NSMutableURLRequest requestWithURL:myurl];

        [connectionReq addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        [connectionReq addValue:@"http://tempuri.org/GetCountryList" forHTTPHeaderField:@"SOAPAction"];
        [connectionReq setHTTPBody: [soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
        [connectionReq addValue:[NSString stringWithFormat:@"%i",[soapMsg length]] forHTTPHeaderField:@"Content-Length"];
        [connectionReq setHTTPMethod:@"POST"];



        NSURLConnection *myConnection=[[NSURLConnection alloc] initWithRequest:connectionReq delegate:self];
        if (myConnection) {
            myWebData=[[NSMutableData alloc]initWithLength:0];
        }
    }

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    NSLog(@"connection error");
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [myWebData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{ 
    [myWebData appendData:data];

}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *str=[[NSString alloc] initWithBytes:[myWebData bytes] length:[myWebData length] encoding:NSStringEncodingConversionAllowLossy];
    NSLog(@"%@",str);
    [str release];

    if(myXMLParser!=nil && [myXMLParser retainCount]>0)
    { 
        myXMLParser.delegate=nil; 
        [myXMLParser release]; 
        myXMLParser=nil; 
    }

    myXMLParser=[[NSXMLParser alloc] initWithData:myWebData];
    myXMLParser.delegate=self;
    [myXMLParser parse];

    [connection release];
    [myWebData release];
}

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

-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if(tempStr!=nil && [tempStr retainCount]>0)
    {  
        [tempStr release]; tempStr=nil;
    }
    tempStr=[[NSString alloc] initWithString:string];
}

-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
///////////////logic here
}

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    //NSLog(@"%@",[countryArray description]);
//  NSLog(@"%@",[stateArray description]);
//  NSLog(@"%@",[cityArray description]);
    //NSLog(@"%@",[tempstate description]);
    /////////////////////          print array if you want in this you will get all data in array ,
    //[(UITableView *)self.view reloadData];
}

hi friend i think you need to learn about nsxmlparser delegate there are three delegate methods

  1. didstartelement
  2. didendelement
  3. foundcharacter

and one suggestion to all if you not answering please do not downvote someone atleast he/she try for this you can add comment if they are wrong

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM