gpt4 book ai didi

iOS - XML pretty-print

转载 作者:可可西里 更新时间:2023-11-01 03:27:53 25 4
gpt4 key购买 nike

我在我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com