2012-05-29 79 views
0

我有一個複雜的長XHTML文件,其中包含CSS。搜索谷歌和在這個網站,我發現有些庫可能是有用的XHTML解析:iPhone解析xhtml + css

  • 的NSXMLParser
  • TBXML
  • 和其他一些

不過,我想知道是否有任何可以將xhtml + css文檔轉換爲NSAttributedString(當然只有文本)的iPhone庫。

我一直在想這個問題,我有一些想法,但我認爲它不會很有效。我的主要想法是通過這個步驟形成:

  • 檢測上XTHML文件中的所有代碼與一個idclass屬性,讓他們在那裏的影響(我不能做到這一點),字符串的範圍。
  • 將所有CSS屬性保存在NSDictionary中,其中有更多NSDictionary對象。事情是這樣的:

    mainDict { 
        object: dictionary { 
         object: @"#00ff00" 
         key: @"color" 
         object: @"1em" 
         key: @"font-size" 
        } 
        key: @"a id" 
        object: anotherDictionary { 
         ... 
        } 
        key: @"another id" 
    } 
    
  • 將這些CSS屬性的NSAttributedString字典屬性字典。

我知道,這是複雜的,我不需要你提供的代碼(當然,如果你提供給它,這將是巨大的),我只希望鏈接庫或,如果它不存在的話,我自己創建一個解析器的建議。

當然,如果您需要更多的信息,請發表評論。

謝謝你!

+0

你想一個接一個,還是一次搞定一切? – 2012-05-29 12:39:05

+0

我不介意,做你喜歡的! – Garoal

+0

@AlbertoSantos你爲什麼這麼問?你有什麼主意嗎? – Garoal

回答

2

這取決於你的需要,如果這會做你想做的,但DTCoreText有一個HTML - > NSAttributedString轉換器。這對於DTCoreText想要/需要做什麼非常具體,但它至少可以指引您朝着正確的方向發展。

1

我將HTML字符串解析爲NSAttributedString的方法是遞歸地將解析後的節點(及其childNodes)附加到NSMutableAttributedString中。

我還沒有準備好在任何地方發佈我的完整代碼。但希望這可以給你一些提示...

的NSString + HTML.h

/* - toHTMLElements 
* parse the string itself into a dictionary collection of htmlelements for following keys 
* : @"attributedString" // html main body 
* : @"insets"   // images and/or videos with range info 
* : @"as"    // href with range info 
* 
*/ 

- (NSMutableDictionary*) toHTMLElements; 

的NSString + HTML.m

- (NSMutableDictionary*) toHTMLElements { 

    // … 
    // handle escape encoding here 
    // assume that NSString* htmlString is the processed string; 
    // … 


    NSMutableDictionary * htmlElements = [[NSMutableDictionary dictionary] retain]; 

    NSMutableAttributedString * attributedString = [[[NSMutableAttributedString alloc] init] autorelease]; 
    NSMutableArray * insets = [NSMutableArray array]; 
    NSMutableArray * as  = [NSMutableArray array]; 

    [htmlElements setObject:attributedString forKey:HTML_ATTRIBUTEDSTRING]; 
    [htmlElements setObject:insets forKey:HTML_INSETS]; 
    [htmlElements setObject:as forKey:HTML_AS]; 


    // parse the HTML with an XML parser 
    // CXXML is a variance of TBXML (http://www.tbxml.co.uk/) which can handle the inline tags such as <span> 
    // code not available to public yet, so write your own inline-tag-enabled HTML/XML parser. 

    CXXML * xml = [CXXML tbxmlWithXMLString:htmlString]; 
    TBXMLElement * root = xml.rootXMLElement; 

    TBXMLElement * next = root->firstChild; 

    while (next != nil) { 
     // 
     // do something here for special treatments if needed 
     // 
     NSString * tagName = [CXXML elementName:next]; 

     [self appendXMLElement:next withAttributes:[HTMLElementAttributes defaultAttributesFor:tagName] toHTMLElements:htmlElements]; 

     next = next->nextSibling; 
    } 

    return [htmlElements autorelease]; 
} 

- (void) appendXMLElement:(TBXMLElement*)aElement withAttributes:(NSDictionary*)parentAttributes toHTMLElements:(NSMutableDictionary*) htmlElements { 

    // do your parse of aElement and its attribute values, 
    // assume NSString * tagAttrString is the parsed html attribute string (either from "style" attribute or css file) for this tag like : width:200px; color:#123456; 
    // let an external HTMLElementAttributes class to handle the attribute updates from the parent node's attributes 

    NSDictionary * tagAttr = [HTMLElementAttributes updateAttributes: parentAttributes withCSSAttributes:tagAttrString]; 

    // create your NSAttributedString styled by tagAttr 
    // create insets such as images/videos or hyper links objects 
    // then update the htmlElements for storage 

    // once this tag is handled, recursively visit and process the current tag's children 

    TBXMLElement * nextChild = aElement->firstChild; 

    while (nextChild != nil) { 
     [self appendXMLElement:nextChild withAttributes:tagAttr toHTMLElements:htmlElements]; 
     nextChild = nextChild->nextSibling; 
    } 
} 
+0

我會嘗試它,它似乎是我在尋找... – Garoal

+0

你能提供一點點的代碼? – Garoal