使用TBXML解析XML文档时,采用的是DOM模式。
TBXML框架需要去下载
下载地址 https://github.com/71squared/TBXML
技术网站 http://www.tbxml.co.uk/TBXML/TBXML_Free.html
需要将TBXML-HEADERS 和 TBXML-Code文件夹添加到工程中。还要注意一下它所依赖的库
Foundation.framework
UIKit.framework
CoreGraphics.framework
libz.dylib
还需要做一些设置,在Objective-C版本中,需要在工程预编译头文件PrefixHeader.pch中添加如下代码:
#import <Foundation/Foundation.h>
#define ARC_ENABLED
工程没有.pch文件需新建一个,去设置里配置一下。
Notes.xml文档
<?xml version="1.0" encoding="UTF-8"?>
<Notes>
<Note id="1">
<Name>赵一</Name>
<Sex>男</Sex>
<Age>40</Age>
</Note>
<Note id="2">
<Name>钱二</Name>
<Sex>女</Sex>
<Age>30</Age>
</Note>
<Note id="3">
<Name>张三</Name>
<Sex>男</Sex>
<Age>25</Age>
</Note>
<Note id="4">
<Name>李四</Name>
<Sex>女</Sex>
<Age>20</Age>
</Note>
</Notes>
#import <Foundation/Foundation.h>
#import "TBXML.h"
@interface NotesTBXMLParser : NSObject
@property (strong, nonatomic) NSMutableArray *notes;
- (void)start:(NSString*)fileName;
@end
#import "NotesTBXMLParser.h"
@implementation NotesTBXMLParser
@synthesize notes = _notes;
- (void)start:(NSString*)fileName
{
_notes = [NSMutableArray new];
TBXML *tbxml = [[TBXML alloc] initWithXMLFile:[fileName stringByAppendingString:@".xml"] error:nil];
TBXMLElement *root = tbxml.rootXMLElement;
if (root) {
TBXMLElement *noteElement = [TBXML childElementNamed:@"Note" parentElement:root];
while (noteElement != nil) {
NSMutableDictionary *dict = [NSMutableDictionary new];
TBXMLElement *NameElement = [TBXML childElementNamed:@"Name" parentElement:noteElement];
if (NameElement != nil) {
NSString *Name = [TBXML textForElement:NameElement];
[dict setValue:Name forKey:@"Name"];
}
TBXMLElement *SexElement = [TBXML childElementNamed:@"Sex" parentElement:noteElement];
if (SexElement != nil) {
NSString *Sex = [TBXML textForElement:SexElement];
[dict setValue:Sex forKey:@"Sex"];
}
TBXMLElement *AgeElement = [TBXML childElementNamed:@"Age" parentElement:noteElement];
if (AgeElement != nil) {
NSString *Age = [TBXML textForElement:AgeElement];
[dict setValue:Age forKey:@"Age"];
}
NSString *_id = [TBXML valueOfAttributeNamed:@"id" forElement:noteElement error:nil];
[dict setValue:_id forKey:@"id"];
[_notes addObject:dict];
noteElement = [TBXML nextSiblingNamed:@"Note" searchFromElement:noteElement];
}
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"reloadViewNotification" object:self.notes userInfo:nil];
self.notes = nil;
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadView:) name:@"reloadViewNotification" object:nil];
NotesTBXMLParser *tbParser = [NotesTBXMLParser new];
[tbParser start:@"Notes"];
}
- (void)reloadView:(NSNotification*)notification
{
NSMutableArray *resList = [notification object];
NSLog(@"%@", resList);
}
Swift代码
在Swift版本中,我们需要在每个TBXML头文件中添加以下内容
#import <Foundation/Foundation.h>
#define ARC_ENABLED
在桥接头文件My-Bridging-Header.h中引入头文件TBXML.h。
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reloadView:", name: "reloadViewNotification", object: nil)
var parser = NotesTBXMLParser()
parser.start("Notes")
}
func reloadView(notification : NSNotification)
{
var resList = notification.object as! NSMutableArray
NSLog("%@", resList)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
import Foundation
class NotesTBXMLParser: NSObject {
private var notes : NSMutableArray!
func start(name : String)
{
self.notes = NSMutableArray()
var tbxml = TBXML(XMLFile: name.stringByAppendingString(".xml"), error: nil)
var root = tbxml.rootXMLElement
if root != nil {
var noteElement = TBXML.childElementNamed("Note", parentElement: root)
while noteElement != nil {
var dict = NSMutableDictionary()
var NameElement = TBXML.childElementNamed("Name", parentElement: noteElement)
if NameElement != nil {
var Name = TBXML.textForElement(NameElement)
dict.setValue(Name, forKey: "Name")
}
var SexElement = TBXML.childElementNamed("Sex", parentElement: noteElement)
if SexElement != nil {
var Sex = TBXML.textForElement(SexElement)
dict.setValue(Sex, forKey: "Sex")
}
var AgeElement = TBXML.childElementNamed("Age", parentElement: noteElement)
if AgeElement != nil {
var Age = TBXML.textForElement(AgeElement)
dict.setValue(Age, forKey: "Age")
}
var id = TBXML.valueOfAttributeNamed("id", forElement: noteElement)
dict.setValue(id, forKey: "id")
self.notes.addObject(dict)
noteElement = TBXML.nextSiblingNamed("Note", searchFromElement: noteElement)
}
}
NSNotificationCenter.defaultCenter().postNotificationName("reloadViewNotification", object: self.notes)
self.notes = nil
}
}