There are two common ways to parse XML data
First Kind
Parsing XML data using Google's Gdataxmlnode
Dom mode parsing is used.
First swallow the XML and then the 1.1-point resolution
The second Kind
Parsing XML data using Apple's own Nsxmlparser
Using the Sax parsing method
Parsing of a tag for a token
First step of Use step
First put the Gdataxml folder into the project
Step Two
Change what's inside bulid setting.
Change header File Search path
Add a path inside the header Search paths/USR/INCLUDE/LIBXML2
Add-LXML2 inside the other Linker flags
Change the GDATAXMLNODE.M file to a non-arc environment
The third step introduces the header file
#import "GDataXML/GDataXMLNode.h"
Code
Load the entire document gdataxmldocument *doc = [[Gdataxmldocument alloc] initwithdata:data options:0Error:nil];Get with elemental gdataxmlelement *rootelement = doc. RootElement;Get all the video elements Nsarray *elements= [rootelement elementsforname:@"Video"];Iterating over the array for (Gdataxmlelement *eleminchelements) {Video *video = [[Video alloc] init];Video. ID= [Elem attributeforname:@"id"]. StringValue;Video. Length= [Elem attributeforname:@"Length"]. StringValue;Video. Name= [Elem attributeforname:@"Name"]. StringValue;Video. Image= [Elem attributeforname:@"Image"]. StringValue;Video. URL= [Elem attributeforname:@"url"]. StringValue;[Self. VideosAddobject:video];}
The second way to use parsing
This method is provided by Apple and requires the proxy method to be set
The proxy method is invoked whenever a tag is encountered
//创建解析器NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];//设置解析器的代理parser.delegateself;//开始解析[parser parse];//这是一个同步操作
Proxy method
/** * will be called when parsing to the beginning of the document * /- (void) Parserdidstartdocument: (Nsxmlparser *) parser{}/** * Parse to the beginning of an element will call * * @param elementname element name * @param attributedict Property Dictionary */- (void) Parser: (Nsxmlparser *) parser didstartelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) QName attributes: (nsdictionary *) attributedict{if([@"Videos"Isequaltostring:elementname])return; Hmvideo *video = [Hmvideo videowithdict:attributedict]; [Self.videos Addobject:video];}/** * Parse to the end of an element will call * * @param elementname element name */- (void) Parser: (Nsxmlparser *) parser didendelement: (NSString *) elementname NamespaceURI: (NSString *) NamespaceURI QualifiedName: (NSString *) qname{}/** * will be called when parsing to the end of the document (parse end) */- (void) Parserdidenddocument: (Nsxmlparser *) parser{}
Two common methods of "iOS development-Network" parsing XML data