xcode 将 xml 转换为 plist

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10920565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 00:31:10  来源:igfitidea点击:

Convert xml to plist

iosxmlxcodeplist

提问by Vins

I have an XML file and I have to convert it into a property list file (plist). I tried the "plutil" command from the terminal, but it don't works well. How can I convert? Thanks a lot

我有一个 XML 文件,我必须将其转换为属性列表文件 (plist)。我从终端尝试了“plutil”命令,但效果不佳。我该如何转换?非常感谢

回答by bkbeachlabs

I accomplished this by writing a formula in excel to create the text of the plist file for you. Its pretty easy.

我通过在 excel 中编写一个公式来为您创建 plist 文件的文本来实现这一点。它很容易。

1) (optional)In XCode, create a plist that has the formatting you want in the end. Save, and go to this in finder. Open it in textWrangler or text edit so that you can see what the tags are for each element in your plist.

1)(可选)在XCode中,创建一个最终具有您想要的格式的plist。保存,然后在查找器中转到这里。在 textWrangler 或 text edit 中打开它,以便您可以查看 plist 中每个元素的标签。

For example, you should find something like:

例如,您应该找到以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">

<dict>

    <key>First Key Name</key>
    <array>
        <string>Before God we are all equally wise</string>
        <string></string>
        <string>Albert Einstein</string>
        <integer>0</integer>
    </array>

    <key>Second Key Name</key>
    <array>
        <string>Do not worry about your troubles.</string>
        <string></string>
        <string>Albert Einstein</string>
        <integer>0</integer>
    </array>

</dict>
</plist>

2) I will assume:- Your spreadsheet represents a description of the same 4 properties for many rows/Objects/Levels - You have each array as a separate row in your spreadsheet - The same properties are being described for every row in the same order. - You will be able to access the items in the array by knowing which index they are stored at. ie, I would get the second Author by looking in the second array at index 2.

2)我将假设:- 您的电子表格表示许多行/对象/级别的相同 4 个属性的描述 - 您将每个数组作为电子表格中的单独行 - 以相同顺序为每一行描述相同的属性. - 您将能够通过知道它们存储在哪个索引来访问数组中的项目。即,我将通过查看索引 2 处的第二个数组来获得第二个作者。

Your spreadsheet should look something like this:

您的电子表格应如下所示:

           A                B            C             D                E            F

     /--(Key Names)--\/-------------------(Properties of Each)-----------------\

1    Level Name      |   Quote        |  Notes  |   Author        |  Favorited  | 
     ____________________________________________________________________________
2    First Key Name  |  Before God... |         | Albert Einstein |      0      |
3    Second Key Name |  Do not wor... |         | Albert Einstein |      0      |
.
.
.

So each of my objects will get its own key in my dictionary and each object is described by 3 Strings and an integer.

所以我的每个对象都会在我的字典中得到它自己的键,每个对象都由 3 个字符串和一个整数描述。

3) The Formula

3) 公式

On the right of the last column in my spreadsheet, I will write a formula that generates the xml code for that row:

在电子表格最后一列的右侧,我将编写一个公式来为该行生成 xml 代码:

(F2) = CONCATENATE ("<key>",A2,"</key> <array> <string>",B2,"</string> <string>",C2,"</string> <string>",D2,"</string> <integer>",E2,"</integer> </array>")

Drag the lower-right corner of this cell down into all the other rows and they will automatically populate the information for each row.

将此单元格的右下角向下拖动到所有其他行中,它们将自动填充每一行的信息。

Similarly, at the bottom of the spreadsheet, under all the rows you can use another formula to concatenate the rows. Say you had 300 rows:

同样,在电子表格底部的所有行下,您可以使用另一个公式来连接行。假设你有 300 行:

(F4) = CONCATENATE (F2:F300) 

Now copy this data into an text file, and add the last few tags and doctype information, which you can copy from above. save with the extension .plist and you're good to go! Add the file back into your xcode project and load it like any other plist.

现在将此数据复制到文本文件中,并添加最后几个标签和文档类型信息,您可以从上面复制这些信息。使用扩展名 .plist 保存,您就可以开始了!将文件添加回您的 xcode 项目并像加载任何其他 plist 一样加载它。