gpt4 book ai didi

iphone - 如何使用 TouchXML 或其他替代方案解析 HTML

转载 作者:行者123 更新时间:2023-11-30 16:03:02 25 4
gpt4 key购买 nike

我尝试使用 TouchXML 解析下面显示的 HTML,但当我尝试提取某些属性时,它不断崩溃。我对解析器世界完全陌生,所以我为自己是一个十足的白痴而道歉。我需要帮助来解析此 HTML。我想要完成的是解析每个属性和值或不解析的内容,然后将它们复制到字符串中。我一直在尝试寻找一个好的解析器来解析 HTML,并且由于 Tidy,我相信 TouchXML 是我见过的最好的解析器。说到 Tidy,我怎样才能先通过 Tidy 运行这个 HTML 然后解析它呢?我不知道该怎么做。这是我到目前为止无法工作的代码,因为它没有从 HTML 中提取我需要的所有内容。任何帮助或建议将不胜感激。谢谢

我当前的代码:

NSMutableArray *res = [[NSMutableArray alloc] init];

// using local resource file
NSString *XMLPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"example.html"];
NSData *XMLData = [NSData dataWithContentsOfFile:XMLPath];
CXMLDocument *doc = [[[CXMLDocument alloc] initWithData:XMLData options:0 error:nil] autorelease];

NSArray *nodes = NULL;

nodes = [doc nodesForXPath:@"//div" error:nil];

for (CXMLElement *node in nodes) {
NSMutableDictionary *item = [[NSMutableDictionary alloc] init];



[item setObject:[[node attributeForName:@"id"] stringValue] forKey:@"id"];

[res addObject:item];
[item release];
}


NSLog(@"%@", res);
[res release];

需要解析的HTML文件:

<html> 
<head>
<base target="_blank" />
</head>
<body style="margin:2;">
<div id="group">
<div id="groupURL"><a href="http://www.example.com/groups">Group URL</a></div>
<img id="grouplogo" src="http://images.example.com/groups/image.png" />
<div id="groupcomputer"><a href="http://www.example.com/groups/page" title="Group Title">Group title this would be here</a></div>
<div id="groupinfos">
<div id="groupinfo-l">Person</div><div id="groupinfo-r">Ralph</div>
<div id="groupinfo-l">Years</div><div id="groupinfo-r">4 years</div>
<div id="groupinfo-l">Salary</div><div id="groupinfo-r">100K</div>
<div id="groupinfo-l">Other</div><div id="groupoth" style="width:15px">other info</div>
</body>
</html>

编辑:我可以使用元素解析器,但我需要知道如何从以下示例中提取人名,在本例中为 Ralph。

<div id="groupinfo-l">Person</div><div id="groupinfo-r">Ralph</div>

最佳答案

我不知道你是否做错了什么,但我建议你使用element parser ,我发现的最好的 XML 和 HTML 解析器。希望这会有所帮助。

关于iphone - 如何使用 TouchXML 或其他替代方案解析 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4484154/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com