如何编写XML文件?

发布于 2024-09-25 21:05:41 字数 134 浏览 4 评论 0原文

我想在我的应用程序中编写一个不太复杂但很大的文件,并能够通过邮件发送它(使用 MFMailComposeViewController) 由于 NSXMLElement 和相关类未移植到 iPhone SDK,我有什么选项来创建 XML 文档? 提前致谢。

I want to write a not so complicated but large file within my app and be able to send it by mail (using MFMailComposeViewController)
Since NSXMLElement and related classes are not ported to iPhone SDK what options do I have for creating XML documents?
Thanks in advance.

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

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

发布评论

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

评论(4

棒棒糖 2024-10-02 21:05:41

我建议使用 KissXML。作者的情况与您类似,并围绕 libxml 创建了一个 NSXML 兼容的 API 包装器。他在博客上此处讨论了选项和决定。

I would recommend using KissXML. The author started in a similar situation as you and created an NSXML compatible API wrapper around libxml. He discusses the options and decisions here on his blog.

盗琴音 2024-10-02 21:05:41

无耻的自我推销:KSXMLWriter

Shameless self-promotion: KSXMLWriter

守望孤独 2024-10-02 21:05:41

尝试使用开源适用于 iOS 的 XML 流编写器

  • 用 Objective-C 编写,单个 .h 。和 .m 文件
  • 一个 @protocol 用于命名空间支持,一个 @protocol 不支持

示例:

// allocate serializer
XMLWriter* xmlWriter = [[XMLWriter alloc]init];

// start writing XML elements
[xmlWriter writeStartElement:@"Root"];
[xmlWriter writeCharacters:@"Text content for root element"];
[xmlWriter writeEndElement];

// get the resulting XML string
NSString* xml = [xmlWriter toString];

这会生成以下 XML 字符串:

<Root>Text content for root element</Root>

Try the open source XML stream writer for iOS:

  • Written in Objective-C, a single .h. and .m file
  • One @protocol for namespace support and one for without

Example:

// allocate serializer
XMLWriter* xmlWriter = [[XMLWriter alloc]init];

// start writing XML elements
[xmlWriter writeStartElement:@"Root"];
[xmlWriter writeCharacters:@"Text content for root element"];
[xmlWriter writeEndElement];

// get the resulting XML string
NSString* xml = [xmlWriter toString];

This produces the following XML string:

<Root>Text content for root element</Root>
十年不长 2024-10-02 21:05:41

这是 NSString 构建的家庭作业。抽象地说,创建一个如下协议:

@protocol XmlExport
-(NSString*)xmlElementName;
-(NSString*)xmlElementData;
-(NSDictionary*)xmlAttributes;
-(NSString*)toXML;
-(NSArray*)xmlSubElements;
@end

确保您保存的所有内容都实现它,并使用如下内容构建 XML:

-(NSString*)toXML {
    NSMutableString *xmlString;
    NSString *returnString;

    /* Opening tag */
    xmlString = [[NSMutableString alloc] initWithFormat:@"<%@", [self xmlElementName]];
    for (NSString *type in [self xmlAttributes]) {
        [xmlString appendFormat:@" %@=\"%@\"", type, [[self xmlAttributes] valueForKey:type]];
    }   
    [xmlString appendString:@">"];

    /* Add subelements */
    for (id<XmlExport> *s in [self xmlSubElements]) {
        [xmlString appendString:[s toXML]];
    }

    /* Data */
    if ([self xmlElementData]) {
        [xmlString appendString:[self xmlElementData]];
    }

    /* Close it up */
    [xmlString appendFormat:@"</%@>", [self xmlElementName]];

    /* Return immutable, free temp memory */
    returnString = [NSString stringWithString:xmlString];
    [xmlString release]; xmlString = nil;

    return returnString;
}

It's a homework exercise in NSString building. Abstractly, create a protocol like:

@protocol XmlExport
-(NSString*)xmlElementName;
-(NSString*)xmlElementData;
-(NSDictionary*)xmlAttributes;
-(NSString*)toXML;
-(NSArray*)xmlSubElements;
@end

Make sure everything you're saving implements it and build the XML with something like the following:

-(NSString*)toXML {
    NSMutableString *xmlString;
    NSString *returnString;

    /* Opening tag */
    xmlString = [[NSMutableString alloc] initWithFormat:@"<%@", [self xmlElementName]];
    for (NSString *type in [self xmlAttributes]) {
        [xmlString appendFormat:@" %@=\"%@\"", type, [[self xmlAttributes] valueForKey:type]];
    }   
    [xmlString appendString:@">"];

    /* Add subelements */
    for (id<XmlExport> *s in [self xmlSubElements]) {
        [xmlString appendString:[s toXML]];
    }

    /* Data */
    if ([self xmlElementData]) {
        [xmlString appendString:[self xmlElementData]];
    }

    /* Close it up */
    [xmlString appendFormat:@"</%@>", [self xmlElementName]];

    /* Return immutable, free temp memory */
    returnString = [NSString stringWithString:xmlString];
    [xmlString release]; xmlString = nil;

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