iOS - XML pretty-print

标签 ios objective-c xml gdataxml

我在我的 iOS 应用程序中使用 GDataXML,并且想要一种简单的方法来格式化和打印 XML 字符串 - “ pretty-print ”

有人知道 Objective C 中的算法,或者可以用我可以翻译的另一种语言运行的算法吗?

最佳答案

可以直接修改GDataXMLNode的源码:

- (NSString *)XMLString {
   ...
   // enable formatting (pretty print / beautifier)
   int format = 1; // changed from 0 to 1
   ...
}

备选方案:

因为我不想直接修改库(出于维护原因),所以我写了那个类来从外部扩展类:

GDataXMLNode+PrettyFormatter.h:

#import "GDataXMLNode.h"
@interface GDataXMLNode (PrettyFormatter)

- (NSString *)XMLStringFormatted;

@end

GDataXMLNode+PrettyFormatter.m:

#import "GDataXMLNode+PrettyFormatter.h"

@implementation GDataXMLNode (PrettyFormatter)

- (NSString *)XMLStringFormatted {

    NSString *str = nil;

    if (xmlNode_ != NULL) {

        xmlBufferPtr buff = xmlBufferCreate();
        if (buff) {

            xmlDocPtr doc = NULL;
            int level = 0;
            // enable formatting (pretty print / beautifier)
            int format = 1;

            int result = xmlNodeDump(buff, doc, xmlNode_, level, format);

            if (result > -1) {
                str = [[[NSString alloc] initWithBytes:(xmlBufferContent(buff))
                                                length:(xmlBufferLength(buff))
                                              encoding:NSUTF8StringEncoding] autorelease];
            }
            xmlBufferFree(buff);
        }
    }

    // remove leading and trailing whitespace
    NSCharacterSet *ws = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSString *trimmed = [str stringByTrimmingCharactersInSet:ws];
    return trimmed;
}

@end

关于iOS - XML pretty-print ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6403083/

相关文章:

ios - 推送通知如何传输到设备?

ios - CloudKit CKError 扩展在 Objective-C 中不可用?

c# - 从网络反序列化 XML

java - 如何通过 Jsoup 使用元素 im :image? 获取 XML

xml - 如何使用 xPath 从命名空间的 xml 中检索特定元素

ios - 退出全屏后 MPMoviePlayerController 缩放模式问题

ios - documents目录下的Core Data sqlite数据库是iCloud自动备份的吗?

ios - 由于 Cocoapods,Unity 无法使用 firebase 分析编译项目

objective-c - -fobjc-nonfragile-abi2 在 LLVM 编译器 2.0 : unknow argument?

objective-c - 对于 ARC,为什么还要使用 @properties?