TBXml empty tag text
-
08-12-2019 - |
문제
I am using TBXML in a project of mine and I was wondering
Is there a way to check if a tag contains no text, for instance
<Description/>
vs
<Description> This is text </Description>
When I use the debugger and type po [TBXML textForElement:groupdescription]
it returns <object returned empty description>
when the tag that was read is the one without text, the other one works perfectly. So my question is, how do I check for that?
Thanks
해결책
If you look at TBXML source you will find
+ (NSString*) textForElement:(TBXMLElement*)aXMLElement {
if (nil == aXMLElement->text) return @"";
return [NSString stringWithCString:&aXMLElement->text[0] encoding:NSUTF8StringEncoding];
}
so,
if you will do po @"" on gdb - you will get "object returned empty description"
I think you should check text length maybe... Actually, TBXML is a light xml parser and doesn't include xml validation etc. That's why it's so fast ;)
다른 팁
Incase others are facing the same problem. TBXML has these class functions
+ (NSString*) textForElement:(TBXMLElement*)aXMLElement {
if (nil == aXMLElement->text) return @"";
return [NSString stringWithCString:&aXMLElement->text[0] encoding:NSUTF8StringEncoding];
}
+ (NSString*) textForElement:(TBXMLElement*)aXMLElement error:(NSError **)error {
// check for nil element
if (nil == aXMLElement) {
*error = [TBXML errorWithCode:D_TBXML_ELEMENT_IS_NIL];
return @"";
}
// check for nil text value
if (nil == aXMLElement->text || strlen(aXMLElement->text) == 0) {
*error = [TBXML errorWithCode:D_TBXML_ELEMENT_TEXT_IS_NIL];
return @"";
}
return [NSString stringWithCString:&aXMLElement->text[0] encoding:NSUTF8StringEncoding];
}
By using the second it does the checking for you. To my knowledge most of the calls like this one have an +error version.
Please note that it needs a **error.