【问题标题】:XMLStreamReader for Objective C iPhone?目标 C iPhone 的 XMLStreamReader?
【发布时间】:2011-01-13 06:57:05
【问题描述】:

我正在使用XMLWriter 来生成 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;
        }
    }

【问题讨论】:

    标签: iphone objective-c xmlreader xmlstreamreader


    【解决方案1】:

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

    头文件:

    #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
    

    【讨论】:

      【解决方案2】:

      NSXMLParser 包含在 Foundation 中,可以满足您的需求。

      【讨论】:

        【解决方案3】:

        您可以使用 libxml2 及其XmlReader API 直接访问源代码。它基于您在上面似乎已经引用的 C# 中的 XmlReader API,但在 C 中。

        我意识到它不是 Objective-c,但您可以轻松地将功能封装在一个可供项目其余部分访问的类中。

        【讨论】:

        • 嗨 bobDevil.. 很好的输入 +1,但 madhup 也添加了代码。所以我标记了他的答案
        猜你喜欢
        • 2011-12-09
        • 2011-05-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多