获取HTML文档的最后一个<p>标记</p>

时间:2011-03-31 10:54:13

标签: html objective-c xml cocoa nsxmlparser

我有一个HTML文档,例如:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head>
    <body style="font-family: Geneva; color: rgb(0, 0, 0); font-size: 12px; word-wrap: break-word; font-weight: normal; font-style: normal; text-decoration: none; ">
        <p style="font-family: LucidaGrande; color: rgb(51, 102, 204); margin-top: 6px; margin-bottom: 6px; word-wrap: break-word; font-weight: normal; font-style: normal; text-decoration: none; ">
            fdskl says: (6:50:04 AM)
        </p>
        <p style="font-family: Arial-ItalicMT; color: rgb(0, 0, 0); margin-left: 36px; margin-top: 6px; margin-bottom: 6px; word-wrap: break-word; font-weight: normal; font-style: normal; text-decoration: none; ">
            Hello
        </p>
        <p style="font-family: LucidaGrande; color: rgb(51, 102, 204); margin-top: 6px; margin-bottom: 6px; word-wrap: break-word; font-weight: normal; font-style: normal; text-decoration: none; ">
            fdskl says: (6:50:18 AM)
        </p>
        <p style="font-family: Arial-ItalicMT; color: rgb(0, 0, 0); margin-left: 36px; margin-top: 6px; margin-bottom: 6px; word-wrap: break-word; font-weight: normal; font-style: normal; text-decoration: none; ">
            How are you?
        </p>
    </body>
</html>

我希望获得此HTML最后一个p标记内的内容。所以在这种情况下,它将是“你好吗?”。使用Cocoa,我该怎么做? 谢谢!

1 个答案:

答案 0 :(得分:0)

您最好的选择是使用NSXMLDocument

NSData *htmlData = ... // get the html data, preferably asynchronously
NSXMLDocument *document = [[[NSXMLDocument alloc] initWithData:htmlData options:NSXMLDocumentTidyHTML error:NULL] autorelease]; 
NSArray *nodes = [document nodesForXPath:@"//body/p" error:NULL];
NSXMLNode *lastP = [nodes lastObject];
NSLog(@"%@", [lastP stringValue]);

如果您希望代码更健壮,您还应该检查错误而不是传递NULL。