私は NSXMLParser を使用して XML ファイルを解析しています。一部の要素の長い説明を解析しますが、一部の要素ではほとんどのテキストをスキップし、最後から一部のテキストのみを取得します。
<communities>
<community>
<name>Newport Bay Club</name>
<primaryImageURL>http://bocaratonrealestatemarket.com/wp-content/uploads/2013/05/newprtbay200px.jpg</primaryImageURL>
<thedescription>Newport Bay Club is one of the most desirable communities in Boca Raton. This beautiful community is in a perfect location on Jog Road north of Clint Moore Road. Newport Bay Club is man-gated 24 hours. Developed between 1988 and 1995, it features finely detailed single family homes that are separated into four subdivisions and surrounded by several serene lakes. Residences range in size from 1,500 to over 3,500 square feet and are offered in a variety of styles. Newport Bay Club residents enjoy amenities such as lighted tennis courts, an Olympic size pool, large clubhouse with meeting rooms for private and community functions, state of the art fitness center with aerobics studio and locker rooms, indoor racquetball courts, basketball courts, library, and a playground for children.</thedescription>
</community>
<community>
<name>Boca Country Club</name>
<primaryImageURL>http://bocaratonrealestatemarket.com/wp-content/uploads/2013/05/bocacountryclub200px.jpg</primaryImageURL>
<thedescription>Just minutes away from beaches, restaurants, shopping, and parks, Boca Country Club is a perfect location for families and professionals. Its 945 single family homes, townhomes, condos and villas are divided into 12 subdivisions and secured by a 24 hour manned gate. Boca Country Club is in an A-Rated public school district, though some residents choose to send their children to one of the local private schools. Tennis and golf membership is available, but it is not required to live in the Boca Country Club community. </thedescription>
</community>
<community>
<name>Woodfield Country Club</name>
<primaryImageURL>http://bocaratonrealestatemarket.com/wp-content/uploads/2013/05/WoodfieldCountryClub200px.jpg</primaryImageURL>
<thedescription>Woodfield Country Club, located in the heart of Boca Raton, is a wonderful private gated community. A championship golf course and tranquil lakes separate the quaint sub communities. Once past the gate, Woodfield’s private back streets are lined with lush tropical landscaping. In the center of the country club’s beautiful grounds lies its exceptional clubhouse. This is where friends and neighbors get together to enjoy one of the 5 individual restaurants, Cascades Spa and Fitness Center, inviting swimming pool, or Youth Activities Center. Woodfield Country Club has an activity for everyone. </thedescription>
</community>
</communities>
この XML ではthedescription
、最初の 2 つのコミュニティから完全なタグを取得しますが、Woodfield Country Club からは取得しませんか? そのコミュニティを XML ファイルの途中または先頭に移動しても。
私のパーサーコード:
-(id) loadXMLByURL:(NSString *)urlString
{
_communities = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:urlString];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser parse];
return self;
}
- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if(!currentNodeContent)
currentNodeContent = [[NSMutableString alloc] initWithString:string];
else
[currentNodeContent appendString:string];
}
- (void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
if ([elementname isEqualToString:@"community"])
{
currentCommunity = [Community alloc];
}
}
- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementname namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementname isEqualToString:@"name"])
{
NSString *trimmed = [currentNodeContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
currentCommunity.name = trimmed;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"primaryImageURL"])
{
NSString *trimmed = [currentNodeContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
currentCommunity.imageURL = trimmed;
NSLog(@"%@ ", trimmed);
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"thedescription"])
{
NSString *trimmed = [currentNodeContent stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
currentCommunity.commDescription = trimmed;
currentNodeContent = nil;
}
if ([elementname isEqualToString:@"community"])
{
[self.communities addObject:currentCommunity];
currentCommunity = nil;
currentNodeContent = nil;
}
}
一部のコミュニティの説明では機能するのに、他の説明では機能しない理由がわかりません。どんな助けでも大歓迎です。
XML ファイルの URL: http://steveedwin.com/test2.xml