解析Web服务数据iOS

时间:2012-06-21 13:42:53

标签: objective-c ios xml web-services nsxmlparser

下午好,

我已经从Web服务下载了数据并且我正在寻找解析数据以便我可以使用它,但我在解析返回的值时遇到问题,下面是采集代码以及任何其他内容之间的任何帮助都是赞赏

 -(IBAction)runNewImport:(id)sender{


recordResults = FALSE;

soapMessage = [NSString stringWithFormat:

               @"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"

               "<s:Envelope \n"

               "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" \n"
               "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" 
               "xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" \n"
               "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" \n"
               "xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"> \n"       


               "<s:Body> \n"

               "<[FUNCTION] xmlns=\"http://tempuri.org/\"/>\n"

               "</s:Body> \n"
               "</s:Envelope>"];


[[NSURLCache sharedURLCache] removeAllCachedResponses];
NSURL *url = [NSURL URLWithString:@"http://[PATH]"];      
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];             
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];          
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];       
[theRequest addValue: @"[FUNCTION]" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];     
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

if(theConnection) {
    webData = [NSMutableData data];
    NSLog(@"%@",webData);
}
else {
    NSLog(@"theConnection is NULL");
}       



}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"DONE. Received Bytes: %d", [webData length]);
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSLog(@"THIS IS THE DATA : %@",theXML);


xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate:self];
[xmlParser setShouldResolveExternalEntities: YES];
[xmlParser parse];


}

-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)
 elementName namespaceURI:(NSString *) 
 namespaceURI qualifiedName:(NSString *)qName
  attributes: (NSDictionary *)attributeDict
{
if( [elementName isEqualToString:@"CODE"])
{
    soapResults = [[NSMutableString alloc] init];
    NSLog(@"%@",soapResults);
    recordResults = TRUE;
}
}

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

{
if( [elementName isEqualToString:@"CODE"])
{
    recordResults = FALSE;
    soapResults = nil;
    }
}

感谢您再次光临,欢迎所有帮助

1 个答案:

答案 0 :(得分:5)

您似乎误解了NSXMLParser的工作方式。我恳请您好好看看Apple XML parsing

中的这份文档
-(void)parser:didStartElement:namespaceURI:qualifiedName:attributes:

当解析器找到一个开放的XML标记时调用此方法,因此它还没有任何数据。在这里,您只需要为要保存的内容分配内存

- (void)parser:foundCharacters:
parser:didStartElement:namespaceURI:qualifiedName:attributes:中找到标记内的数据时,会调用

,这是您首先存储该数据的位置,以便稍后可以将其保存在下一个方法中所需的对象上:

-(void)parser:didEndElement:namespaceURI:qualifiedName
解析器遇到结束标记()时调用

。现在,您将数据保存在parser:foundCharacters:的变量中,是时候将其保存在对象中了。

编辑好吧,让我们尝试通过在这些方法中添加代码示例来细分它:

假设您的XML看起来像这样;

<person>
    <lastName>Doe</lastName>
    <firstName>John</firstName>
    <address>
        <street>100 Main Street</street>
        <city>Somewhere</city>
    </address>
</person>

当然,您希望拥有一个Person类,其属性 lastName firstName addressDictionary 包含街道城市。要保留所有Person,您需要 personArray

现在我们有了结构,这里是解析部分。 请注意,为了更好地理解,我将分别编写每个 If 块。

.h文件

@property (nonatomic, retain) Person *currentPerson;
@property (nonatomic, retain) NSMutableString *currentElement;
@property (nonatomic, retain) NSMutableDictionary *addressDic; //to be saved to person.addressDictionary when finished
@property (nonatomic, retain) NSMutableArray *personArray;

.m文件

- (void)parserDidStartDocument:(NSXMLParser *)parser 
{
    personArray = [[NSMutableArray alloc] init];
}

- (void) parser: (NSXMLParser *) parser didStartElement: (NSString *) elementName
 namespaceURI: (NSString *) namespaceURI qualifiedName: (NSString *) qName attributes: (NSDictionary *) attributeDict
{
    if ([elementName isEqualToString:@"Person"])
    {
    currentPerson = [[Person alloc] init]; //
    return;
    }
    if ([elementName isEqualToString:@"lastName"])
    {
        currentElement = [[NSMutableString alloc] init];
        return;
    }
    if ([elementName isEqualToString:@"firstName"])
    {
        currentElement = [[NSMutableString alloc] init];
        return;
    }
    if ([elementName isEqualToString:@"address"])
    {
        adressDic = [[NSMutableDictionary alloc] init];
        return;
    }
    if ([elementName isEqualToString:@"street"])
    {
        currentElement = [[NSMutableString alloc] init];
        return;
    }
    if ([elementName isEqualToString:@"city"])
    {
        currentElement = [[NSMutableString alloc] init];
        return;
    }
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [currentElement appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"Person"])
    {
    [personArray addObject:currentPerson];
    [currentPerson release];
    return;
    }
    if ([elementName isEqualToString:@"lastName"])
    {
        currentPerson.lastName = currentElement;
        [currentElement release]; currentElement = nil;
        return;
    }
    if ([elementName isEqualToString:@"firstName"])
    {
        currentPerson.firstName = currentElement;
        [currentElement release]; currentElement = nil;
        return;
    }
    if ([elementName isEqualToString:@"address"])
    {
        currentPerson.addressDictionary = addresDic;
        [addressDic release];
        return;
    }
    if ([elementName isEqualToString:@"street"])
    {
        [addressDic setObject:currentElement forKey:@"street"];
        [currentElement release]; currentElement = nil;
        return;
    }
    if ([elementName isEqualToString:@"city"])
    {
        [addressDic setObject:currentElement forKey:@"city"];
        [currentElement release]; currentElement = nil;
        return;
    }
}

完成所有操作后,parserDidEndDocument:被调用。既然你的数组中包含了所有Person,那么就随心所欲地做任何事情

- (void)parserDidEndDocument:(NSXMLParser *)parser
{
     for (Person *person in personArray)
     {
          NSLog(@"Person name:%@", person.firstName);
          NSLog(@"Person lastname:%@", person.lastName);
     }
}