Converting NSDictionary to XML

后端 未结 3 1879
滥情空心
滥情空心 2020-12-11 07:52

I need to Post data in XML format. The server accepts a specific xml format. I don\'t want to write the xml by hand, what i want to do is create a NSMutableDictionary<

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-11 08:31

    The short answer is: No, there is no built-in ability to do this in the Cocoa libraries.

    Because you're writing rather than parsing, and presumably dealing with a limited universe of possible tags, the code to output XML is actually not that complicated. It should just be a simple method in your Invoice object, something like:

    - (NSString*) postStringInXMLFormat
    {
        NSMutableString* returnValue = [[NSMutableString alloc] init];
        if([self email])
        {
            [returnValue appendString:@""];
            [returnValue appendString:[self email]];
            [returnValue appendString:@""];
        }
        if([self invoice_date])
        ...
    

    and so on. At the end return

    [NSString stringWithString:returnValue]
    

    There are plenty of third-party projects out there that try to generalize this process; several of them are listed in this answer:

    Xml serialization library for iPhone Apps

    But if all you're looking to do is create a single, stable format that your server side will recognize, and you don't have a ridiculous number of entities to convert, it's probably less work to roll your own.

提交回复
热议问题