iOS - XML 漂亮打印

发布于 2024-11-16 07:08:50 字数 114 浏览 3 评论 0原文

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

有谁知道 Objective C 中的算法,或者可以翻译的另一种语言中的算法吗?

I am using GDataXML in my iOS application and want a simple way to format and print an XML string - "pretty print"

Does anyone know of an algorithm in Objective C, or one that works in another language I can translate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北陌 2024-11-23 07:08:50

您可以直接修改 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。米:

#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

You can modify the source code of GDataXMLNode direcly:

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

Alternative:

As I didn't want to modify the library directly (for maintenance reasons), I wrote that category to extend the class from outside:

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
维持三分热 2024-11-23 07:08:50

我已经使用 HTML Tidy (http://tidy.sourceforge.net/) 来完成类似的事情。它是一个 C 库,因此只要您熟悉 C,就可以相当轻松地链接到 Objective C 运行时并从中调用。C++ API 可以从 Objective C++ 调用,因此如果您熟悉 C++,可能会更容易使用目标C++。

我没有使用过 C 或 C++ 绑定;我通过 Ruby 或 Python 完成了它,但它们都是相同的库。它将直接读取 XML(以及可能不干净的 HTML),并且具有简单且漂亮的打印选项。

I've used HTML Tidy (http://tidy.sourceforge.net/) for things like this. It's a C library so can be linked in to and called from an Objective C runtime fairly easily as long as you're comfortable with C. The C++ API is callable from Objective C++ so that might be easier to use if you're comfortable with Objective C++.

I've not used the C or C++ bindings; I did it via Ruby or Python but it's all the same lib. It will read straight XML (as well as potentially dirty HTML) and it has both simple and pretty print options.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文