DOM是什么时候准备好的,如何枚举它的元素? [英] When is the DOM ready and how to enumerate its elements?
问题描述
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
DOMDocument * myDOMDocument = [[self.webview mainFrame] DOMDocument];
看起来像一个很好的起点,但是我发现WebScriptObject类引用有点神秘。显然我不想评估一些Javascript来获取链接。我想直接阅读DOM。
如何查找DOM中的哪些节点是链接,并获取他们指向的地址?
查找图像的DOMNodes
请参阅walkNodeTree @ http://cocoadev.com/wiki/DOMCore
- 完整的示例来查找图像节点,获取他们的src和make nsimages
@implementation DDAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[self.webview.mainFrame loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@http://dominik.pich.info/Home.html]]];
}
- (void)webView:(WebView *)sender didFinishLoadForFrame :( WebFrame *)frame {
DOMDocument * myDOMDocument = [[self.webview mainFrame] DOMDocument];
NSMutableArray * imgs = [NSMutableArray数组];
[self walkNodeTree:myDOMDocument imgsCollected:imgs];
//坏代码,演示
NSMutableArray * nsImages = [NSMutableArray数组]; (int i = 0; i< img.attributes.length; i ++){
DOMNode * attr = [img.attributes item:$ {
for(DOMNode * img in imgs)一世];
NSLog(@%@,attr.nodeName);
if([attr.nodeName.lowercaseString isEqualToString:@src]){
NSString * urlstring = [attr nodeValue];
NSURL * url = [NSURL URLWithString:urlstring relativeToURL:[NSURL URLWithString:@http://dominik.pich.info/]];
NSImage * nsimg = [[NSImage alloc] initWithContentsOfURL:url];
if(nsimg)
[nsImages addObject:nsimg];
}
}
}
NSLog(@%@,nsImages);
}
- (void)walkNodeTree:(DOMNode *)parent imgsCollected:(NSMutableArray *)imgs {
DOMNodeList * nodeList = [parent childNodes];
unsigned i,length = [nodeList length]; (i = 0; i< length; i ++)
{
DOMNode * node = [nodeList item:i];
NSLog(@%@,node.nodeName);
if([node.nodeName.lowercaseString isEqualToString:@img]){
[imgs addObject:node];
}
else {
// recurse
[self walkNodeTree:node imgsCollected:imgs];
}
}
}
@end
Using a Webview, I would like to find the links inside this page.
-(void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
DOMDocument *myDOMDocument = [[self.webview mainFrame] DOMDocument];
looks like a good starting point, but I find the WebScriptObject Class Reference a little cryptic. Clearly I don't want to evaluate some Javascript to get the links. I want to directly read the DOM.
How may I find which nodes in the DOM are links, and get the address they are pointing to ?
Find the DOMNodes for images
see walkNodeTree @ http://cocoadev.com/wiki/DOMCore
-- complete sample to find image nodes, get their src and make nsimages
@implementation DDAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[self.webview.mainFrame loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://dominik.pich.info/Home.html"]]];
}
-(void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
DOMDocument *myDOMDocument = [[self.webview mainFrame] DOMDocument];
NSMutableArray *imgs = [NSMutableArray array];
[self walkNodeTree:myDOMDocument imgsCollected:imgs];
//bad code, demo
NSMutableArray *nsImages = [NSMutableArray array];
for (DOMNode *img in imgs) {
for(int i = 0; i < img.attributes.length; i++) {
DOMNode *attr = [img.attributes item:i];
NSLog(@"%@", attr.nodeName);
if([attr.nodeName.lowercaseString isEqualToString:@"src"]) {
NSString *urlstring = [attr nodeValue];
NSURL *url = [NSURL URLWithString:urlstring relativeToURL:[NSURL URLWithString:@"http://dominik.pich.info/"]];
NSImage *nsimg = [[NSImage alloc] initWithContentsOfURL:url];
if(nsimg)
[nsImages addObject:nsimg];
}
}
}
NSLog(@"%@", nsImages);
}
- (void)walkNodeTree:(DOMNode*)parent imgsCollected:(NSMutableArray*)imgs {
DOMNodeList *nodeList = [parent childNodes];
unsigned i, length = [nodeList length];
for (i = 0; i < length; i++) {
DOMNode *node = [nodeList item:i];
NSLog(@"%@", node.nodeName);
if([node.nodeName.lowercaseString isEqualToString:@"img"]) {
[imgs addObject:node];
}
else {
//recurse
[self walkNodeTree:node imgsCollected:imgs];
}
}
}
@end
这篇关于DOM是什么时候准备好的,如何枚举它的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!