I'm having problems including TBXML in my project.
The guide tells me to include four files, TBXML.h
, TBXML.m
, NSDataAdditions.h
, and NSDataAdditions.m
, but the latter two are nowhere to be found in the Github repo.
I tried running the sample project TBXML-Books in the hopes of copying how TBXML was imported into the project, but it doesn't build successfully in Xcode 5 either. It can't find libTBXML-iOS.a
.
Anybody help? Thanks in advance.
Get TBXML.h
and TBXML.m
from the Github repo and add them into your project. Those two are the only files you need.
In your project's Target > Build Phases, add the compiler flag -fno-objc-arc
to TBXML.m
.
TBXML *sourceXML = [[TBXML alloc] initWithXMLFile:@"dictionary.xml" error:nil];
You can alloc-init with the other init instance methods, or do it class-method style (I didn't include the deprecated methods):
- (id)initWithXMLString:(NSString*)aXMLString error:(NSError **)error;
- (id)initWithXMLData:(NSData*)aData error:(NSError **)error;
- (id)initWithXMLFile:(NSString*)aXMLFile error:(NSError **)error;
- (id)initWithXMLFile:(NSString*)aXMLFile fileExtension:(NSString*)aFileExtension error:(NSError **)error;
+ (id)newTBXMLWithXMLString:(NSString*)aXMLString error:(NSError **)error;
+ (id)newTBXMLWithXMLData:(NSData*)aData error:(NSError **)error;
+ (id)newTBXMLWithXMLFile:(NSString*)aXMLFile error:(NSError **)error;
+ (id)newTBXMLWithXMLFile:(NSString*)aXMLFile fileExtension:(NSString*)aFileExtension error:(NSError **)error;
<dictionary>
<entry id="">
<text></text>
</entry>
<entry id="">
<text></text>
</entry>
</dictionary>
TBXMLElement *rootElement = sourceXML.rootXMLElement;
TBXMLElement *entryElement = [TBXML childElementNamed:@"entry" parentElement:rootElement];
NSString *id = [TBXML valueOfAttributeNamed:@"id" forElement:entryElement];
TBXMLElement *textElement = [TBXML childElementNamed:@"text" parentElement:entryElement];
NSString *text = [TBXML textForElement:textElement];
If I wanted to print out the text inside every <text>
element inside every <entry>
, this is what I'll do:
TBXML *sourceXML = [[TBXML alloc] initWithXMLFile:@"dictionary.xml" error:nil];
TBXMLElement *rootElement = sourceXML.rootXMLElement;
TBXMLElement *entryElement = [TBXML childElementNamed:@"entry" parentElement:rootElement];
do {
TBXMLElement *textElement = [TBXML childElementNamed:@"text" parentElement:entryElement];
NSString *word = [TBXML textForElement:textElement];
NSLog(@"%@", word);
} while ((entryElement = entryElement->nextSibling) != nil);
I haven't personally tried out traversing the attributes but I assume you can do something like entryElement->firstAttribute
, as shown in the old guide. You can also just look at TBXML.h
for how to go about it.