IOS JSON/XML解析

<pre name="code" class="objc">/*---------------------------XML_SAX解析-------------------------------*/

//XML SAX解析
- (IBAction)XML_SAX:(id)sender
{
    NSData *da = [[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"megage" ofType:@"txt"]];
    //第一步,创建xml解析
    NSXMLParser *Parserdata = [[NSXMLParser alloc] initWithData:da];
    //第二步设置代理
    Parserdata.delegate = self;
    //第三步 启动解析
    [Parserdata parse];
    
    NSLog(@"点我");
}
/*---------------------------XML_SAX代理方法-------------------------------*/
//開始解析------------->>>启动解析的时候走代理方法
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
    //1.在開始解析的时候初始化接收信息的数组,为了不占用内存
    self.megArray = [NSMutableArray array];

}

//開始解析某个节点的时候
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    //小1.把他的节点接收起来
    self.elementString = elementName;
    if([elementName isEqualToString:@"message"])
    {
        Recorce *meg = [[Recorce alloc] init];
        [self.megArray addObject:meg];
        
    }
}
//找到内容(节点)
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    Recorce *meg = [self.megArray lastObject];
    //通过节点加入内容
    [meg setValue:string forKey:self.elementString];
    
}
//结束解析某个节点
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    self.elementString = nil;
}

//完毕解析
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
    
    for (Recorce *me in self.megArray) {
        NSLog(@"%@",me);
    }
    
    
}
//解析错误发生的时候
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
    
}
- (IBAction)XML_DOM:(id)sender
{
    NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"megage" ofType:@"txt"]];
    //获取数据
    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
    //找到根节点
    GDataXMLElement *Rootelement = document.rootElement;
    self.megArray = [NSMutableArray array];
    //遍历根节点中的子节点,
    for ( GDataXMLElement *em in Rootelement.children )
    {
        message *meg = [[message alloc] init];
        for (GDataXMLElement *GE in em.children)
        {
            [meg setValue:GE.stringValue forKey:GE.name];
        }
        [self.megArray addObject:meg];
        NSLog(@"%@",meg);
    }
    
}

- (IBAction)JSON_KIT:(id)sender
{
    //获得路径
    NSData *data = [[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"content" ofType:@"txt"]];
    //获得数据(第三方)
    NSArray *dataArray = [data objectFromJSONData];
    NSLog(@"%@",dataArray);
    
}


- (IBAction)JSON:(id)sender
{
    NSData *data = [[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"content" ofType:@"txt"]];
    NSArray *dataArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];


 
   

版权声明:本文博客原创文章,博客,未经同意,不得转载。

你可能感兴趣的:(xml解析)