How to merge multiple plist files into one?
Asked Answered
S

3

5

Just to start, I really have no idea what Im doing. I was given this task for an internship, and am really learning as I go. I have multiple plist files, they consist of around 22 items each, and list values of colors. I need to merge all of these files into one, and am really not sure how to go about it. I have a certain structure I need to go by, and really Im not sure how to go about it. I was told to open the plists in texteditor and then paste all of the raw code into one text file, this doesn't seem to work as I only end up getting the values for the first plist I pasted into the text file. Any help would be nice. Thanks.

Snaffle answered 26/2, 2016 at 20:52 Comment(1)
Look at NSPropertyListSerialization. The class can convert property list files to Foundation collection types and vice versa.Cherilyncherilynn
P
1

There are a number of ways to handle this. By default a plist is a special form of XML file. If you figure out the syntax you can in fact use a text editor to merge the contents of multiple files together, but you need to make sure you get it right.

A plist file has a specific header for the entire file. You could not just copy/paste multiple plists together because then they would have that header repeated.

The next way to do it is programmatically. If you can figure out the type of outer collection these files contain (probably an array or a dictionary) then you could write a few lines of code that read in each of the plists as arrays, combines them using NSArray code (assuming they contain arrays of colors) and then save the combined array back to a new plist. As vadian says you can also use the NSPropertyListSerialization class. Thats a more general-pupose way of handling plist files, but it's also more complex and harder to figure out.

A third way to do it is in Xcode. If you right-click on a plist file and select "open in Xcode" it should give you Xcode's property list editor. You can then copy and paste the contents of the files together and save the results to a new file.

Platonic answered 26/2, 2016 at 21:34 Comment(2)
thanks for the answer! for me copying and pasting the source code into my structure that was given worked the best! Check out my answer.Snaffle
If I answered your question you should accept the answer. Upvotes are also gratefully accepted.Platonic
R
21

Assume your from.plist contains keys 1, 2 and to.plist contains 2, 3

Run this:

/usr/libexec/PlistBuddy -x -c "Merge from.plist" to.plist

to.plist will contain 1, 2, 3

Rama answered 13/4, 2016 at 19:10 Comment(0)
P
1

There are a number of ways to handle this. By default a plist is a special form of XML file. If you figure out the syntax you can in fact use a text editor to merge the contents of multiple files together, but you need to make sure you get it right.

A plist file has a specific header for the entire file. You could not just copy/paste multiple plists together because then they would have that header repeated.

The next way to do it is programmatically. If you can figure out the type of outer collection these files contain (probably an array or a dictionary) then you could write a few lines of code that read in each of the plists as arrays, combines them using NSArray code (assuming they contain arrays of colors) and then save the combined array back to a new plist. As vadian says you can also use the NSPropertyListSerialization class. Thats a more general-pupose way of handling plist files, but it's also more complex and harder to figure out.

A third way to do it is in Xcode. If you right-click on a plist file and select "open in Xcode" it should give you Xcode's property list editor. You can then copy and paste the contents of the files together and save the results to a new file.

Platonic answered 26/2, 2016 at 21:34 Comment(2)
thanks for the answer! for me copying and pasting the source code into my structure that was given worked the best! Check out my answer.Snaffle
If I answered your question you should accept the answer. Upvotes are also gratefully accepted.Platonic
S
0

I figured it out!! First create the structure, or use the template given to you. I suggest opening this template/ structure in Xcode, as it makes it easier to switch between viewing the list as a plist and source code. Open your template as a source code. Then open each of your plists in text editor, and copy and paste the code from your plists into the appropriate area in your templates source code, then you can view it in Xcode as a property list to make sure it's correct. The only thing you have to be careful about here is making sure you are getting no errors. Otherwise this works great!!

Snaffle answered 26/2, 2016 at 21:36 Comment(1)
Editing the raw XML file is more error-prone. It's easier to get a formatting problem that way. Xcode is safer, since it deals with the data structures (arrays, dictionaries, strings, integers, dates, etc.) and won't let you create a malformed file. If Xcode allowed you to shift-click and select a range of contents it would be the hands-down best way to do this, but it doesn't.Platonic

© 2022 - 2024 — McMap. All rights reserved.