【问题标题】:Updated guide for TBXML: How to include TBXML in Xcode 5, for iOS 7更新了 TBXML 指南:How to include TBXML in Xcode 5, for iOS 7
【发布时间】:2013-10-17 02:49:03
【问题描述】:

我在项目中遇到了包括TBXML 在内的问题。

  1. guide 告诉我包含四个文件,TBXML.hTBXML.mNSDataAdditions.hNSDataAdditions.m,但后两个在 the Github repo 中找不到。

  2. 我尝试运行示例项目TBXML-Books,希望复制TBXML 是如何导入到项目中的,但它在Xcode 5 中也没有成功构建。找不到libTBXML-iOS.a

有人帮忙吗?提前致谢。

【问题讨论】:

标签: ios objective-c xml xcode tbxml


【解决方案1】:

将 TBXML 包含到您的项目中

  1. the Github repo 获取TBXML.hTBXML.m 并将它们添加到您的项目中。这两个是您唯一需要的文件。

  2. 在项目的 Target > Build Phases 中,将编译器标志 -fno-objc-arc 添加到 TBXML.m

加载 XML 文档

TBXML *sourceXML = [[TBXML alloc] initWithXMLFile:@"dictionary.xml" error:nil];

您可以将 alloc-init 与其他 init 实例方法一起使用,或者使用类方法样式(我没有包含已弃用的方法):

- (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;

示例 XML 结构

<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];

遍历未知元素/属性

如果我想打印每个&lt;entry&gt; 中每个&lt;text&gt; 元素中的文本,我会这样做:

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);

我没有亲自尝试过遍历属性,但我假设您可以执行entryElement-&gt;firstAttribute 之类的操作,如the old guide 所示。您也可以查看TBXML.h 了解如何操作。

【讨论】:

  • 如何添加编译器标志?我转到构建阶段并查看文件 - 然后呢?我现在看到“编译器标志”和最右边的 -fno-objc-arc。但是红色的'!'仍然存在相同的错误。还有一套吗?
  • @user3741598 对于 Xcode 5,转到构建设置并搜索“Apple LLVM 5.0 - 语言 - Objective-C”。然后,在您的目标项目中将 Objective-C 自动引用计数设置为 NO。
  • 谢谢。我确实在一个新的、干净的项目中解决了这个问题(然后遇到了一个已知的 TBXML 问题,带有一个虚假的“使用 stringWithString ...冗余”警告(这显然不仅仅是一个警告,因为调用无法读取 XML :-) )。但是会在旧项目上尝试您的建议。
  • 上述解决方案在 Xcode 7.1 中对我不起作用。
  • 将此指南用于 xcode 7 或更高版本:stackoverflow.com/questions/25828208/…
【解决方案2】:

如果你还没有,我建议你使用 cocoapods。

http://cocoapods.org/?q=tbxml

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多