iPhone에서 HTML 구문 분석
누구나 HTML 구문 분석을 위해 C 또는 Objective-C 라이브러리를 추천 할 수 있습니까? 유효성을 검사하지 못하는 지저분한 HTML 코드를 처리해야합니다.
그러한 라이브러리가 존재합니까, 아니면 정규 표현식을 사용하는 것이 더 낫습니까?
나는 hpple을 사용하여 지저분한 HTML을 구문 분석하는 데 매우 유용하다는 것을 알았 습니다. Hpple 프로젝트는 HTML 구문 분석을위한 XPathQuery 라이브러리의 Objective-C 래퍼입니다. 이를 사용하여 XPath 쿼리를 보내고 결과를받을 수 있습니다.
요구 사항 :
-프로젝트에 libxml2 포함 추가
- 메뉴 프로젝트-> 프로젝트 설정 편집
- "헤더 검색 경로"설정 검색
- 새 검색 경로 "$ {SDKROOT} / usr / include / libxml2"추가
- 재귀 옵션 활성화
-프로젝트에 libxml2 라이브러리 추가
- 메뉴 프로젝트-> 프로젝트 설정 편집
- "기타 링커 플래그"설정 검색
- 새 검색 플래그 "-lxml2"추가
-부터 hpple GET 다음의 소스 코드는 프로젝트에 추가 파일 :
- TFpple.h
- TFpple.m
- TFppleElement.h
- TFppleElement.m
- XPathQuery.h
- XPathQuery.m
-w3school XPath Tutorial 을 따라 가면서 XPath 언어에 익숙해 지십시오.
코드 예
#import "TFHpple.h"
NSData *data = [[NSData alloc] initWithContentsOfFile:@"example.html"];
// Create parser
xpathParser = [[TFHpple alloc] initWithHTMLData:data];
//Get all the cells of the 2nd row of the 3rd table
NSArray *elements = [xpathParser searchWithXPathQuery:@"//table[3]/tr[2]/td"];
// Access the first cell
TFHppleElement *element = [elements objectAtIndex:0];
// Get the text within the cell tag
NSString *content = [element content];
[xpathParser release];
[data release];
알려진 문제
hpple은 또 다른 래퍼 인 XPathQuery에 대한 래퍼이므로이 옵션은 아마도 가장 효율적이지 않습니다. 프로젝트에서 성능이 문제라면 hpple 및 xpathquery 라이브러리 코드를 기반으로 자체 경량 솔루션을 코딩하는 것이 좋습니다.
libxml2.2
SDK에 포함 된 것처럼 보이며 libxml/HTMLparser.h
다음을 주장합니다.
이 모듈은 XML 파서와 호환되는 API를 사용하여 HTML 4.0 비 검증 파서를 구현합니다. 사양 관점에서 심각하게 깨졌더라도 "실제"HTML을 구문 분석 할 수 있어야합니다.
그게 내가 필요한 것 같아서 아마도 그것을 사용할 것입니다.
누군가가 멋진 XPath 파서를 검색하여 여기에 와서 TFHpple을 사용하는 경우를 대비하여 TFHpple은 XPathQuery를 사용합니다. 이것은 꽤 좋지만 메모리 누수가 있습니다.
* PerformXPathQuery 함수에서 노드가 nil 인 것으로 확인되면 정리하기 전에 점프합니다.
그래서이 코드를 볼 수 있습니다. 두 개의 정리 라인을 추가하십시오.
xmlNodeSetPtr nodes = xpathObj->nodesetval;
if (!nodes)
{
NSLog(@"Nodes was nil.");
/* Cleanup */
xmlXPathFreeObject(xpathObj);
xmlXPathFreeContext(xpathCtx);
return nil;
}
많은 구문 분석을 수행하는 경우 악의적 인 누출입니다. 자 .... 어떻게 내 밤을 되 찾을 수 있습니까 :-)
libxml에 대한 경량 래퍼를 작성했는데 유용 할 수 있습니다.
This probably depends on how messy the HTML is and what you want to extract. But usually Tidy does quite a good job. It is written in C and I guess you should be able to build and statically link it for the iPhone. You can easily install the command line version and test the results first.
You may want to check out ElementParser. It provides "just enough" parsing of HTML and XML. Nice interfaces make walking around XML / HTML documents very straightforward. http://touchtank.wordpress.com/
How about using the Webkit component, and possibly third party packages such as jquery for tasks such as these? Wouldn't it be possible to fetch the html data in an invisible component and take advantage of the very mature selectors of the javascript frameworks?
Google's GData Objective-C API reimplements NSXMLElement and other related classes that Apple removed from the iPhone SDK. You can find it here http://code.google.com/p/gdata-objectivec-client/. I've used it for dealing messaging via Jabber. Of course if your HTML is malformed (missing closing tags) this might not help much.
We use Convertigo to parse HTML on the server side and return a clean and neat JSON web services to our Mobile Apps
참고URL : https://stackoverflow.com/questions/405749/parsing-html-on-the-iphone
'Development Tip' 카테고리의 다른 글
하위 프로세스 변경 디렉터리 (0) | 2020.11.06 |
---|---|
typescript 입력 onchange event.target.value (0) | 2020.11.06 |
모든 배열이 C #에서 구현하는 인터페이스는 무엇입니까? (0) | 2020.11.06 |
How to bring a gRPC defined API to the web browser (0) | 2020.11.05 |
Is using flexible array members in C bad practice? (0) | 2020.11.05 |