2013-08-01 52 views
3

我想讀一個plist到我的應用程序中。我想嘗試創建一棵樹。如何閱讀iPhone中的plist文件

plist文件內容:

<plist version="1.0"> 
    <array> 
    <dict> 
     <key>Name</key> 
     <string>Test Webservice</string> 
     <key>child</key> 
     <array> 
     <dict> 
     <key>child</key> 
     <array> 
     <dict> 
      <key>child</key> 
      <array> 
      <dict> 
      <key>id</key> 
      <integer>4291</integer> 
      <key>name</key> 
      <string>1.1.1</string> 
      </dict> 
      <dict> 
      <key>id</key> 
      <integer>4292</integer> 
      <key>name</key> 
      <string>1.1.2</string> 
      </dict> 
      </array> 
      <key>id</key> 
      <integer>4290</integer> 
      <key>name</key> 
      <string>1.1</string> 
     </dict> 
     <dict> 
      <key>child</key> 
      <array> 
      <dict> 
      <key>id</key> 
      <integer>4294</integer> 
      <key>name</key> 
      <string>1.2.1</string> 
      </dict> 
      </array> 
      <key>id</key> 
      <integer>4293</integer> 
      <key>name</key> 
      <string>1.2</string> 
     </dict> 
     </array> 
     <key>id</key> 
     <integer>4287</integer> 
     <key>name</key> 
     <string>1</string> 
     </dict> 
     <dict> 
     <key>child</key> 
     <array> 
     <dict> 
      <key>child</key> 
      <array> 
      <dict> 
      <key>child</key> 
      <array> 
      <dict> 
       <key>id</key> 
       <integer>4297</integer> 
       <key>name</key> 
       <string>2.1.1.1</string> 
      </dict> 
      </array> 
      <key>id</key> 
      <integer>4296</integer> 
      <key>name</key> 
      <string>2.1.1</string> 
      </dict> 
      </array> 
      <key>id</key> 
      <integer>4295</integer> 
      <key>name</key> 
      <string>2.1</string> 
     </dict> 
     </array> 
     <key>id</key> 
     <integer>4288</integer> 
     <key>name</key> 
     <string>2</string> 
     </dict> 
     <dict> 
     <key>id</key> 
     <integer>4289</integer> 
     <key>name</key> 
     <string>3</string> 
     </dict> 
     </array> 
     <key>id</key> 
     <integer>4286</integer> 
    </dict> 
    </array> 
    </plist> 
+0

你沒有具體,閱讀plist到哪裏?泰伯維? –

+0

我想讀表plist在表格視圖.. –

+0

如何獲得 ??請幫助我 –

回答

5

按照給定的片段從plist中讀取數據

NSString *strplistPath = [[NSBundle mainBundle] pathForResource:<plistName> ofType:@"plist"]; 

// read property list into memory as an NSData object 
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:strplistPath]; 
NSString *strerrorDesc = nil; 
NSPropertyListFormat plistFormat; 
// convert static property liost into dictionary object 
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&plistFormat errorDescription:&strerrorDesc]; 
if (!temp) { 
    NSLog(@"Error reading plist: %@, format: %d", strerrorDesc, plistFormat); 
} else { 
    // assign values 
    NSMutableArray *plistArray = [NSMutableArray arrayWithArray:[temp objectForKey:key]]; 
} 

其中鍵 - >從plist中訪問特定領域

喜歡編程!

+0

我想閱讀所有的孩子節點 –

+0

我怎樣才能讀取所有的節點? –

+0

簽出此代碼。這是返回一個數組的形式。所以你可以使用數組執行所有你想要的操作 –

1

第一件事首先,在頭文件中聲明對您的plist屬性

@property (strong, nonatomic) NSArray *content; 

然後合成它實現文件

@synthesize content = _content; 

然後,你必須申報在實現文件陣列。像這樣的東西

-(NSArray *)content 
{ 
if (!_content) { 
    _content = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]]; 
} 
return _content; 
} 

然後你必須聲明數據源。類似以下的東西

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
// Return the number of rows in the section. 
return [self.content count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

cell.textLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"city"]; 
cell.detailTextLabel.text = [[self.content objectAtIndex:indexPath.row] valueForKey:@"state"]; 
return cell; 
} 

這就是簡單的說法。

希望這會有所幫助

+0

Sir Sir How to get from ??請幫幫我。 是第一個元素。例如。 .....