适用于 Objective C iPhone 的 XMLStreamReader?

2024-04-18

我在用XML编写器 http://code.google.com/p/xswi/用于生成 xml。现在我想用一些阅读器库/框架来阅读这个 xml。是否有任何可用的补充框架/库。

我目前正在寻找使用 TouchXML 库来读取此内容,但它无法按预期方式工作,因为它不支持流读取。

我想做类似的事情:

XmlReader pReader = XmlTextReader.Create(pPath);

    while (pReader.Read()){

        switch (pReader.LocalName){
            case EXPEL_DEVICES:
            {
                //if ((pImportFlags & (int)ExportClass.Devices) != 0)
                //{
                for (pReader.ReadToFollowing(LOCAL_NAME, NAMESPACE_EXPORT);
                     !pReader.EOF && pReader.LocalName == @"NAME"; )
                {
                    if (!pReader.ReadToFollowing(DEVICE_ID, NAMESPACE_EXPORT))
                        throw new AException(DEVICE_ID);
                    NSString *value = pReader.ReadElementContentAsString();
                }
            }
                break;
        }
    }

在失去了我自己价值 50 的声誉之后,我最终使用了 libxml2 并制作了我的 XMLStreamReader 类,如下所示,我希望我能早点找到它:P。请注意,为了使用它,我们需要将 libxml2.dylib 包含在我们的框架中,并在构建设置中添加-lxml2 into 其他链接器标志 and in 标头搜索路径 add /usr/include/libxml2

头文件:

#import <Foundation/Foundation.h>
#import <libxml/xmlreader.h>

@interface XMLStreamReader : NSObject {
    xmlTextReaderPtr xmlReader;
}

@property (nonatomic, readonly, assign) BOOL eof;
@property (nonatomic, readonly, retain) NSString *localName;
@property (nonatomic, readonly, assign) xmlElementType nodeType;
@property (nonatomic, readonly, assign) BOOL read;
@property (nonatomic, readonly, assign) BOOL readElementContentAsBoolean;
@property (nonatomic, readonly, retain) NSString *readElementContentAsString;

- (void) close;
- (id) getAttribute:(NSString *) paramName;
- (id) initWithPath:(NSString *) path;
@end

实施文件:

#import "XMLStreamReader.h"

@implementation XMLStreamReader

@dynamic eof;
@dynamic localName;
@dynamic nodeType;
@dynamic read;
@dynamic readElementContentAsBoolean;
@dynamic readElementContentAsString;

- (void) dealloc{
    xmlFreeTextReader(xmlReader);
    [super dealloc];
}

/**
 * xmlTextReaderClose:
 * @reader:  the xmlTextReaderPtr used
 *
 * This method releases any resources allocated by the current instance
 * changes the state to Closed and close any underlying input.
 *
 * Returns 0 or -1 in case of error
 */
- (void) close{
    xmlTextReaderClose(xmlReader);
}

/**
 * @reader:  the xmlTextReaderPtr used
 * @name: the qualified name of the attribute.
 *
 * Provides the value of the attribute with the specified qualified name.
 *
 * Returns a string containing the value of the specified attribute, or NULL
 *    in case of error. The string must be deallocated by the caller.
 */
- (id) getAttribute:(NSString *) paramName{
    xmlChar *attribute = xmlTextReaderGetAttribute(xmlReader, (xmlChar *)[paramName UTF8String]);

    if(attribute != NULL){
        NSString *rtString = [NSString stringWithUTF8String:(const char *)attribute];
        free(attribute);
        return rtString;
    }
    return NULL;
}

/**
 * Checks if, the reader has reached to the end of file
 * 'EOF' is not used as it is already defined in stdio.h
 * as '#define  EOF (-1)'
 */
- (BOOL) eof{
    return xmlTextReaderReadState(xmlReader) == XML_TEXTREADER_MODE_EOF;
}

/**
 * Initializing the xml stream reader with some uri
 * or local path.
 */
- (id) initWithPath:(NSString *) path{
    if(self = [super init]){
        xmlReader = xmlNewTextReaderFilename([path UTF8String]);
        if(xmlReader == NULL)
            return nil;
    }
    return self;
}

/**
 * @reader:  the xmlTextReaderPtr used
 *
 * The local name of the node.
 *
 * Returns the local name or NULL if not available,
 *   if non NULL it need to be freed by the caller.
 */
- (NSString *) localName{
    xmlChar *lclName = xmlTextReaderLocalName(xmlReader);

    if(lclName != NULL){
        NSString *rtString = [NSString stringWithUTF8String:(const char *)lclName];
        free(lclName);
        return rtString;
    }
    return NULL;
}

- (xmlElementType) nodeType{
    return xmlTextReaderNodeType(xmlReader);
}

/**
 * @reader:  the xmlTextReaderPtr used
 *
 *  Moves the position of the current instance to the next node in
 *  the stream, exposing its properties.
 *
 *  Returns 1 if the node was read successfully, 0 if there is no more
 *          nodes to read, or -1 in case of error
 */
- (BOOL) read{
    return xmlTextReaderRead(xmlReader);
}

/**
 * @reader:  the xmlTextReaderPtr used
 *
 * Reads the contents of an element or a text node as a Boolean.
 *
 * Returns a string containing the contents of the Element or Text node,
 *         or NULL if the reader is positioned on any other type of node.
 *         The string must be deallocated by the caller.
 */
- (void) readElementContentAsBoolean{
    return [[self readElementContentAsString] boolValue];
}

/**
 * @reader:  the xmlTextReaderPtr used
 *
 * Reads the contents of an element or a text node as a string.
 *
 * Returns a string containing the contents of the Element or Text node,
 *         or NULL if the reader is positioned on any other type of node.
 *         The string must be deallocated by the caller.
 */
- (NSString *) readElementContentAsString{
    xmlChar *content = xmlTextReaderReadString(xmlReader);

    if(content != NULL){
        NSString *rtString = [NSString stringWithUTF8String:(const char *)content];
        free(content);
        return rtString;
    }
    return NULL;
}

/**
 * @reader:  the xmlTextReaderPtr used
 * @localName:  the local name of the attribute.
 * @namespaceURI:  the namespace URI of the attribute.
 *
 * Moves the position of the current instance to the attribute with the
 * specified local name and namespace URI.
 *
 * Returns 1 in case of success, -1 in case of error, 0 if not found
 */
- (int) readToFollowing:(NSString *) localname namespace:(NSString *) namespaceURI{
    return xmlTextReaderMoveToAttributeNs(xmlReader, (xmlChar *)[localname UTF8String], (xmlChar *)[namespaceURI UTF8String]);
}

@end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

适用于 Objective C iPhone 的 XMLStreamReader? 的相关文章

  • UISplitViewController - 推送模态视图

    使用 UISplitViewController 时推送模态视图的最佳实践是什么 您会从 RootViewController DetailViewController 还是直接从应用程序委托推送 理想情况下 我想要实现的功能是在基于某些条
  • UIView 子类不会自动调整大小

    我一直在寻找有关调整大小的背景信息 但找不到太多 我知道我需要设置autoresizesSubviews在超级视图和autoresizingMask在子视图上 我已经这样做了 并且我的 UIImageViews 正确调整了大小 但我的自定义
  • 沿着预定路径移动图像?

    是否可以通过按下 iphone SDK 中的按钮来将图像设置为沿着预定路径运动 我不是在寻找任何奇特的东西 我正在研究一个简单的概念 但这会节省大量动画工作 是的 您可以通过创建一个路径来为任何 CALayer 制作动画CAKeyframe
  • iPhone 快照,包括键盘

    我正在寻找拍摄整个 iPhone 屏幕 包括键盘 的正确方法 我找到了一些截取屏幕的代码 CGRect screenCaptureRect UIScreen mainScreen bounds UIView viewWhereYouWant
  • 我可以知道 requireGestureRecognizerToFail 到底会做什么吗?

    谁能告诉我下面的代码行到底会做什么 我已经提到过Apples https developer apple com library ios documentation uikit reference UIGestureRecognizer C
  • iPhone Developer' 与任何有效的、未过期的证书/私钥对不匹配 - 但我正在创建 iPad 应用程序 [重复]

    这个问题在这里已经有答案了 可能的重复 代码签名错误 身份 iPhone Developer 与默认钥匙串中的任何有效证书 私钥对不匹配 https stackoverflow com questions 2108503 code sign
  • 对使用phonegap和钛的质疑[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 最近我听说了 PhoneGap 和 Titanium 移动网络应用程序的开发 我分析了这两个 Web 应用程序 并了解了如何使用它们以
  • iPhone,如何隐藏标签栏按钮?

    如何隐藏单个标签栏按钮 我已经搜索过 但什么也没找到 只找到了完整的栏 我已经取得了一些进展 但仍然遇到问题 此代码位于我的应用程序委托中 带有选项卡栏的出口 我在其中调用它viewDidLoad选项卡栏中显示的第一个视图的视图 void
  • 在 UIWebView 中隐藏键盘

    有没有办法让 UIWebView 关闭所有关联的输入控件 例如键盘 选择器 还没有在带有选择器的网络视图中尝试过 但这绝对可以消除键盘 theWebView endEditing YES
  • SpriteKit 碰撞检测中 SKSpriteNode 之间的间隙

    我已经尝试解决这个问题很长一段时间了 我有一个具有简单平台物理原理的游戏 其中玩家跌倒在一个方块上 这可以阻止他跌倒 这是可行的 但是玩家停止的位置和实际对象 精灵节点的位置之间存在明显的差距 这是一个屏幕截图 它应该是不言自明的 clas
  • 如何从通讯录 ios 以编程方式编辑电话号码值

    我正在尝试在 iOS 中以编程方式替换特定联系人的特定电话号码 获取联系人表单地址簿 我不知道为什么我无法保存新的电话号码并刷新地址簿以显示更改 我正在这样做 BOOL changeContactPhoneNumber NSString p
  • 如何将设备令牌和应用程序版本发送到服务器

    我已经实现将设备令牌和应用程序版本发送到 serverm 它在模拟器 硬编码数据 中工作正常 但在设备中无法工作 任何形式的帮助将不胜感激 先感谢您 这是代码 void application UIApplication applicati
  • iOS 以编程方式将 AVI 转换为 MP4 格式

    我的应用程序中有一个查询 因为我想将 AVI 格式的视频转换为 MP4 电影格式 所以有没有什么方法可以以编程方式执行此操作 任何代码片段将不胜感激 你需要使用AVAssetExportSession将视频转换为 mp4格式 下面方法转换
  • iPhone UI 带有 Tableview 或 Scrollview? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • 创建一个包含 UIViewController 的 UIViewController

    我有一个UIViewController这允许我在视图中显示一些文本 我想添加一个输入法而不将其直接添加到此视图控制器中 此输入法将是一个按钮或一个UITextField 这个输入法会很多 但是每次都会用一个 从设置中选择它 所以我不会有一
  • 如何在 Swift 中调用 Objective-C 实例类型方法?

    我有一个 Objective C 类 如下所示 interface CustomObjectHavingData NSObject property nonatomic strong NSData objWithData instancet
  • iphone opencv - 模板匹配

    我已经在我的 iphone 项目中实现了这个 OpenCV 构建 http aptogo co uk 2011 09 opencv framework for ios http aptogo co uk 2011 09 opencv fra
  • 使用 Objective C 将 RGB 彩色图像更改为灰度图像

    我正在开发一个将彩色图像更改为灰度图像的应用程序 然而 有些图片显示出来是错误的 我不知道代码有什么问题 也许我输入的参数有误 请帮忙 UIImage c UIImage imageNamed downRed png CGImageRef
  • 观察 UIDatePicker 的变化

    我注意到没有委托来观察 UIDatePicker 中的变化 有没有一种方法可以在不确认任何内容的情况下检测选择器中何时进行更改 例如它旋转并落在新数字上的那一刻 我希望能够检测到这一点 我考虑过关键值观察 但我不认为有一个属性会立即改变 您
  • 如何连续关闭 2 个模态视图控制器?

    我有 2 个以模态方式呈现的视图控制器 A presents B which presents C 当我解雇C时 我也想解雇B 但我不知道该怎么做 解雇C self dismissModalViewControllerAnimated YE

随机推荐