Parse/Display XML from a POST Method [duplicate]

2019-08-29 12:48发布

This question already has an answer here:

Ok,so on my iOS app I have a HTTP Post method that returns xml, like this

<?xml version="1.0" encoding="utf-8"?>
<geocode>
    <suburb>Sydney</suburb>
    <state>NSW</state>
</geocode>

It returns through a HTTP POST method that gets the Latitude and Longtitude

What I what to do is display this XML on a button or on a label. But not the code just like Bold Sydney and less for NSW. I have the code saved as

NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *strdata=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

So I usually call on the strdata to get the XML but it displays it in its code form and thats the problem. I looked at using NSXMLParser but I couldnt figure out how to use it.

1条回答
小情绪 Triste *
2楼-- · 2019-08-29 12:58

Declare a property like:

@porperty (nonatomic, strong) NSXMLParser  *parser;

And Implement a method like:

- (void)parseXML
{
   NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
   _parser = [[NSXMLParser alloc] initWithData:urlData];
   [_parser setDelegate:self];
   [_parser parse];
}

And implement the following NSXMLParser Delegate methods:

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

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

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

Check the following links:

  1. RSS Reader
  2. NSXMLParser Class Reference
查看更多
登录 后发表回答