首页 > 代码库 > IOS JSON/XML解析

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 = http://www.mamicode.com/[[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"content" ofType:@"txt"]];>

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


IOS JSON/XML解析