如何在课堂上添加NSXML解析器?

时间:2011-04-01 04:24:39

标签: iphone

我想在我的Objective-C类中添加NSXML解析器并解析XML文件

<。>文件中的

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];
}

2 个答案:

答案 0 :(得分:6)

朋友,我想你需要了解nsxmlparser委托 有三种委托方法

  1. didstartelement
  2. didendelement
  3. foundcharacter
  4. 并且如果你没有回答,请向所有人提出一个建议请不要至少投票给某人他/她尝试这样做你可以添加评论,如果他们错了

答案 1 :(得分:0)