将NSDictionary转换为XML [英] Converting NSDictionary to XML

查看:107
本文介绍了将NSDictionary转换为XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以XML格式发布数据。服务器接受特定的xml格式。我不想手工编写xml,我想要做的是创建一个 NSMutableDictionary 填充它并从 NSMutableDictionary 将其转换为XML。

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 populate it and from NSMutableDictionary convert it XML.

我用这个:

[NSPropertyListSerialization dataWithPropertyList:data format:NSPropertyListXMLFormat_v1_0 options:0

样本返回为:

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0">
<dict>
<key>email</key>
<string>me@n.net</string>
<key>invoice_date</key>
<string>2012-10-11T10:35:09Z</string>
<key>invoice_lines</key>
<array>
    <dict>
        <key>product_id</key>
        <integer>1021493</integer>
        <key>quantity</key>
        <real>1</real>
        <key>retail_price</key>
        <real>110</real>
    </dict>
</array>
<key>payments</key>
<array>
    <dict>
        <key>amount</key>
        <real>288.5</real>
    </dict>
    <dict>
        <key>payment_type_id</key>
        <integer>1</integer>
    </dict>
</array>


以上格式无法从服务器读取。

The above format is not readable from the server.

服务器需要像这样的xml提要。

The server need an xml feed like this.

  <invoice>
      <invoice_date>2012-10-11T10:35:09Z</invoice_date>
      <email>me@n.net</email>
        <invoice_lines type="array">
         <invoice_line>
       <product_id>1021505</product_id>
       <quantity>1</quantity>
       <retail_price>45</retail_price>
     </invoice_line>
  </invoice_lines>
  <payments type="array">
    <payment>
      <amount>288.5</amount>
     </payment>
  </payments>

</invoice>

是否可以从 NSDictionary

谢谢!

推荐答案

简短回答是:不,没有内置的能力在Cocoa库中执行此操作。

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

因为您正在编写而不是解析,并且可能处理有限的宇宙对于可能的标签,输出XML的代码实际上并不复杂。它应该只是Invoice对象中的一个简单方法,如:

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:@"<email>"];
        [returnValue appendString:[self email]];
        [returnValue appendString:@"</email>"];
    }
    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:

适用于iPhone应用程序的Xml序列化库

但是,如果你想做的就是创建一个稳定的格式,你的服务器端将会认识到,并且你没有可笑的实体来转换,你可能不那么自愿。

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.

这篇关于将NSDictionary转换为XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆