Jomoka
Jomoka

Reputation: 157

NSXMLParser, parsing xml issue

I want to parse the description enclosed in the [description] [p] blah blah [/description] [/p]

 <item>
 <title>
 Jon Greene named associate director for strategic planning and development at institute
 </title>
 <link>
 http://www.vtnews.vt.edu/articles/2013/03/031513-ictas-greenepromotion.html
 </link>


 <description>
 <p>In this new position, Jon Greene will be responsible for strategic research development of multimillion-dollar, interdisciplinary proposals at the Institute for Critical Technology and Applied Science,</p>
 </description>



 <pubDate>Fri, 15 Mar 2013 00:00:00 -0400</pubDate>
 <guid isPermaLink="true">
 http://www.vtnews.vt.edu/articles/2013/03/031513-ictas-greenepromotion.html
 </guid>
 <enclosure url="http://www.vtnews.vt.edu/articles/2009/10/images/M_09783greene-jpg.jpg" length="27715" type="image/jpeg"/>
 <category>
 Institute for Critical Technology and Applied Science
 </category>
 <category>College of Engineering</category>
 <category>Research</category>
 <category>National Capital Region</category>
 </item>

I am using the delegate methods for NSXMLParser:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
    attributes:(NSDictionary *)attributeDict {


- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

To get the title i can just do this in the didEndElement method:

if ([elementName isEqualToString:@"title"]){
        self.aArticle.title = self.currentElementValue;
    }

However this does not work when i try @"p" Just wondering how I can access < p > node with the entire description.

Is there a better way/solution to this?

When i do an NSLog printing of strings in foundCharacters i get part of this:

<
2013-03-18 00:42:31.978 newsFeed[67052:c09] string is p
2013-03-18 00:42:31.978 newsFeed[67052:c09] string is >
2013-03-18 00:42:31.979 newsFeed[67052:c09] string is Tysor will work closely with national security thrust leader Jon Greene and with cognition and communication thrust leader Jeff Reed, a professor of electrical and computer engineering in the College of Engineering and director of Wireless@VT.
2013-03-18 00:42:31.979 newsFeed[67052:c09] string is  
2013-03-18 00:42:31.979 newsFeed[67052:c09] string is <
2013-03-18 00:42:31.979 newsFeed[67052:c09] string is /p
2013-03-18 00:42:31.979 newsFeed[67052:c09] string is >

The only way I can think of right now is some how keep track of where < p > is found in method foundCharacters, and have a count variable that checks if we reach the value within the enclosing < p > tags.

Upvotes: 0

Views: 619

Answers (2)

βhargavḯ
βhargavḯ

Reputation: 9836

Just try this way

VC.m

NSMutableString *xmlString;

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)nameSpaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if (!xmlString)
        xmlString = [[NSMutableString alloc] init];
    else
        [xmlString setString:@""];
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    [xmlString appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{    
    if ([elementName isEqualToString:@"p"])
    {
        if (![xmlString isEqual:@""])
        {
            NSLog(@"%@",xmlString)'
            [SomeArray addObject:xmlString];
        }
    }

    //Release string
    [xmlString release];
    xmlString = nil;
}

Upvotes: 1

Dharmbir Singh
Dharmbir Singh

Reputation: 17535

Please try to use this one ..

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
     // ----------- Current Element Name means it is equal to <p> tag -----------
     if([self.currentElementName isEqualToString:@"p"])
     {
         NSLog(@"Value %@",string);
     }
}

Upvotes: 0

Related Questions