0

I have been reading through heaps of the ios docs from Apple, and almost every bit of code to do with xml on there requires either creating a file with xml data in it or receiving xml over a connection then altering it.

I would like to know if there is a way to just create some xml.. maybe in a string, then convert it into a NSData object.. I know how to pass a NSString into a NSData object.. thats not so much of a big deal.. the painful part at the moment is getting some clear concise examples on how to create my own xml without all the other stuff that Apple says you need.. I.e. file or return data.

I currently have no code.. what I will likely do is create a method with some parameters that can receive some information and then pass them in to hopefully create a nice xml string/data object.

4

3 に答える 3

1

Cocoa オブジェクトを XML に変換することについて言及している場合は、試すライブラリが多数あります。簡単なグーグルでWonderXMLを立ち上げました。これまで使用したことはありませんが、これはあなたが望むことをしているように思えます。

正確な XML 形式を気にしない場合は、ほとんどの Cocoa オブジェクトを XML にシリアル化できるNSKeyedArchiverorを (jtbandes が書いているように) いつでも使用できます。NSPropertyListSerialization他の環境や言語で XML を読むつもりなら、この方法はお勧めしません。

于 2012-02-28T01:19:29.450 に答える
0

次を使用できます。

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag

...特定のスキーマを持つXMLドキュメントである.plistにNSDictionaryを書き出す。含まれるすべてのオブジェクトは、NSData、NSDate、NSNumber、NSString、NSArray、またはNSDictionaryのインスタンスである必要があります。

- (id)initWithContentsOfFile:(NSString *)pathXML/plistを辞書に読み戻すために使用します。

iOSプロジェクトの情報ファイルはプロパティリスト/plistです。テキストエディタで表示すると、次のようになります。

<?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>CFBundleDevelopmentRegion</key>
    <string>English</string>
    <key>CFBundleDisplayName</key>
    <string>App Name</string>
    <key>CFBundleExecutable</key>
    <string>${EXECUTABLE_NAME}</string>
    <key>CFBundleIconFiles</key>
    <array>
        <string>Icon.png</string>
        <string>Icon@2x.png</string>
        <string>Icon-iPad.png</string>
        <string>Icon-Small-50.png</string>
        <string>Icon-Small.png</string>
        <string>Icon-Small@2x.png</string>
    </array>
</dict>
</plist>
于 2012-02-28T01:21:02.007 に答える
0

最も簡単な方法はNSPropertyListSerializationを使用することです。それにディクショナリ (または任意のプロパティ リスト オブジェクト) を渡すと、Apple のプロパティ リスト形式で XML (文字列またはデータとして) を生成できます。

于 2012-02-28T01:16:45.877 に答える