Two XML parsing methods in iPhone: NSXMLParser and GDataXMLNode

Source: Internet
Author: User

IPhoneMediumXMLResolution MethodNSXMLParserAndGDataXMLNodeIs the content to be introduced in this article.NSXMLParserAndGDataXMLNodeThese two methods are commonly used for parsingXMLThere are two methods, they are based on different APIs:

1. tree-based API: the processing method of this API is to regard the XML structure as a Tree, and then treat each part of the Tree as an object for processing, this is what we call the DOM (Document Object Model) method. The iPhone SDK contains a libxml2 Framework to parse the DOM. Google's GDataXML is also based on libxml2. Therefore, before using GDataXML, you must first import libxml2.

2. Event-driven API: This method is usually used to parse based events. The SAX parsing method is representative of this method. This method can also be used to parse XML developed on the iPhone. However, this is not an attribute of the Iphone SDK, but an Objective-C function. In Objectvie-C, there is a class NSXMLParser dedicated to parsing XML.

How to Use NSXMLParser and CGataXML:

NSXMLParser Parsing Method

NSXMLParser is mainly implemented by NSXMLParserDelegate.

 
 
  1. - (void)viewDidLoad {  
  2. [super viewDidLoad];  
  3. //........  
  4. NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://lab.xxxxxx.com/xxx/xxx.xml"]];  
  5. [parser setDelegate:self];  
  6. [parser parse];   
  7. for (int i = 0; i < [newsArray count]; i++) {  
  8. NSLog(@"%@",[newsArray objectAtIndex:i]);  
  9. }  

With just a few words, I began to parse it. How can I get the data in XML?

Find it in the proxy!

 
 
  1. #pragma mark NSMXLParser Delegate Methods  
  2. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName   
  3. namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{  
  4. if ([elementName isEqualToString:@"docTitle"]) {  
  5. if (!newsArray) {  
  6. newsArray = [[NSMutableArray alloc] init];  
  7. return;  
  8. }  
  9. }  
  10. }  
  11. - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{  
  12.  
  13. if (!currentString) {  
  14. currentString = [[NSMutableString alloc] init];  
  15. }  
  16. [currentString appendString:string];  
  17. [currentString setString:[currentString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];  
  18. }  
  19. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName   
  20. namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{  
  21. if ([elementName isEqualToString:@"docTitle"]) {  
  22. [newsArray addObject:currentString];  
  23. }  
  24. [currentString release];  
  25. currentString = nil;  

First, let's talk about the program running process. When parser initializes and executes the parse Statement ([parser parse]), the program will jump to the proxy method and take the first proxy method. The first proxy method will traverse the entire xml and identify the element name (elementName) in the xml. When I find the information I want (docTitle, create an array to store the information. Of course, the first proxy has not started to store the information.

Next we will take the second proxy, which will store the information we are looking for in the first proxy in currentString. Here, I added a processing statement for the obtained string [currentString setString: [currentString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceAndNewlineCharacterSet], it can remove spaces and line breaks at the beginning of the string because the xml file format is not standard ).

The third proxy method stores the information we obtain in the array.

Of course, the three steps in the process of running the program are not strictly executed and will be executed repeatedly, but the general process is like this.

GDataXMLNode for DOM Parsing

GDataXMLNode is a third-party file based on libxml2. it has powerful functions.

 
 
  1. NSError* error = nil;  
  2. NSString *documentStr = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.xxxx.com/x/xxx.xml"] 
  3. encoding:NSUTF8StringEncoding error:&error];  
  4. GDataXMLDocument* XMLdocument = [[GDataXMLDocument alloc] initWithXMLString:documentStr options:0 error:&error];  
  5. [documentStr release];  
  6. GDataXMLElement* rootElement = [XMLdocument rootElement];  
  7. NSArray *newsArray = [rootElement elementsForName:@"docTitle"];  
  8. for (int i = 0; i < [newsArray count]; i++) {  
  9. NSLog(@"%@",[[newsArray objectAtIndex:i] stringValue]);  

In this case, the function is similar to the NSXMLParser mentioned above. Of course, the object of GDataXMLElement is stored in this array instead of a string.

In the DOM parsing method, the XML file or link is first converted into an object, which is a GDataXMLDocument object, and each part of the object is considered as an object. For example, rootElement is a GDataXMLElement object, it is the root of the entire XML tree and contains all information about the XML. We can directly find the docTitle object under the rootElement and put it in the array because there may be more than one such object ). We need to obtain the content in the docTitle object <docTitle> helloIPhone</DocTitle> the stringValue of this object is.

Summary:IPhoneMediumXMLResolution MethodNSXMLParserAndGDataXMLNodeI hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.