我使用TFHpple一些麻煩,第一個層次,所以這裏是: 我想解析以下行:獲取使用TFHpple
<div class=\"head\" style=\"height: 69.89px; line-height: 69.89px;\">
<div class=\"cell editable\" style=\"width: 135px;\"contenteditable=\"true\">
<p> 1</p>
</div>
<div class=\"cell\" style=\"width: 135px;\" contenteditable=\"false\">
<p>2</p>
</div>
</div>
<div style=\"height: 69.89px; line-height: 69.89px;\" class=\"head\">
<div class=\"cell\" style=\"width: 135px; text-align: left;\"contenteditable=\"false\">
<p>3 </p>
</div>
<div class=\"cell\" style=\"width: 135px;\" contenteditable=\"false\">
<p>4</p>
</div>
</div>
<div style=\"height: 69.89px; line-height: 69.89px;\" class=\"\">
<div class=\"cell\" style=\"width: 135px;\" contenteditable=\"false\">
<p>5</p>
</div>
<div class=\"cell\" style=\"width: 135px;\" contenteditable=\"false\">
<p>6</p>
</div>
</div>
現在我想提出的第一級的div
「元素「(對不起,我不知道正確的術語)。 所以我試圖通過簡單地將/div
作爲xPath到searchWithXPathQuery
方法來實現,但它根本找不到任何東西。
我的第二個解決方案是嘗試使用這種路徑://div[@class=\"head\"]
,但也允許[@class=\"\"]
,但我甚至不知道是否有可能。 (我想這樣做,因爲我需要的元素與數據中的排列順序相同)
所以,這裏是我的問題,是否有一個特定的原因,爲什麼TFHpple不會工作與/div
? 如果現在只需要第一級div
,那麼是否可以使用xPath對屬性的值進行謂詞(這裏是屬性class
)? (以及如何?我現在看了很多,找不到任何東西)
感謝您的幫助。
PS:如果有幫助,這是我用它來嘗試解析數據的代碼,它首先將包含字符串self.material.Text
在:
NSData * data = [self.material.Text dataUsingEncoding:NSUnicodeStringEncoding];
TFHpple * tableParser = [TFHpple hppleWithHTMLData:data];
NSString * firstXPath = @"/div";
NSArray<TFHppleElement *> * tableHeader = [tableParser searchWithXPathQuery:firstXPath];
NSLog(@"We found : %d", tableHeader.count);
感謝您答案,它確實解決了我的一部分問題,但事實是,有時'class'屬性在空時會丟失(我無法解決這個問題,我從Web平臺獲取文件),有沒有辦法我仍然可以接受這個元素? (相當於'/ div'的東西可能?) –
@HuguesDuvillier,如果它是空的,這個解決方案也可以工作。在XPath 1.0中,空節點的字符串值是一個空字符串(在XPath 2.0中稍有變化)。如果你還想檢查空格,請使用'normalize-string(@class)'。 – Abel