简体   繁体   English

NSXMLParser,解析xml问题

[英]NSXMLParser, parsing xml issue

I want to parse the description enclosed in the [description] [p] blah blah [/description] [/p] 我想解析[description] [p]等等[/ 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 : 我正在使用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: 要获得标题,我可以在didEndElement方法中执行以下操作:

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

Is there a better way/solution to this? 有没有更好的方法/解决方案?

When i do an NSLog printing of strings in foundCharacters i get part of this: 当我在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 >

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. 我现在唯一能想到的方法是,如何跟踪在foundCharacters方法中找到< p >位置,并拥有一个count变量,该变量检查我们是否达到封闭的< p >标记内的值。

Just try this way 只是尝试这种方式

VC.m VC

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM