下記のHTMLをTouchXMLで解析しようとしていますが、特定の属性を抽出しようとするとクラッシュすることがあります。私は完全にパーサーの世界に新しいので、私は完全なばかだと謝罪します。私はこのHTMLを解析するのに助けが必要です。私が達成しようとしているのは、各属性と値を解析するか、それを文字列にコピーするかということです。私はHTMLを解析するのに良いパーサを見つけようとしてきましたが、私はTouchXMLがTidyのために私が見た中で最高だと信じています。 Tidyといえば、まずこのHTMLをTidyで実行して解析する方法はありますか?私はこれを行う方法がわかりません。ここまでは、私がHTMLから必要なものすべてを引き出していないために動作しないコードがあります。どんな助けやアドバイスも大歓迎です。おかげTouchXMLなどで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>
編集:私は、Elementパーサを使用することができますが、私はから人の名前を抽出する方法を知っておく必要がありますこの例ではRalphとなる次の例があります。
<div id="groupinfo-l">Person</div><div id="groupinfo-r">Ralph</div>
私はElement Parserを試しましたが、特定のデータを取得できません。たとえば、「groupURL」リンクのテキストを取得しようとしているとします。私はそれを得ることができない。私はURLをうまく取得できますが、リンクのGroup URL Titleだけを取得することはできません。ここに私が持っていたコードがあります:\t要素* aTag = [document selectElement:@ "a"]; \t NSString * href = [aタグ属性:@ "href"]; \t NSLog(@ "%@"、href); – 0SX
私は自分のMacではないので、正確なコードを与えることはできませんが、 "groupURL"要素のテキストを取得する場合は、[yourElement contentsText]または[yourElement getChildsContentsText:@ "your child" ]。とにかく、明日私はあなたに正確な解決策を与えることができるでしょう。 – ender
チップを入手してくれてありがとう、私はテキストを得ることができるかどうかわかります。あなたが覚えていれば、あなたはまだ明日の正確なコードを投稿することができます気にしない場合。ありがとう – 0SX