0

我正在使用 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从前两个社区获得了完整的标签,但不是伍德菲尔德乡村俱乐部?即使我将该社区移动到 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

4

0 回答 0