NSXMLParser,解析xml问题

时间:2013-03-18 04:52:18

标签: ios xml parsing nsxmlparser

我想解析[描述] [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>

我正在使用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

要获得标题,我可以在didEndElement方法中执行此操作:

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

但是当我尝试@"p"时,这不起作用 只是想知道如何使用整个描述访问< p >节点。

对此有更好的解决方法吗?

当我在foundCharacters中进行NSLog字符串打印时,我得到了一部分内容:

<
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 >

我现在能想到的唯一方法是如何跟踪方法foundCharacters中找到< p >的位置,并有一个count变量来检查我们是否达到了封闭式< p >中的值标签。

2 个答案:

答案 0 :(得分:1)

试试这种方式

<强> 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;
}

答案 1 :(得分:0)

请尝试使用这个..

- (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);
     }
}