我一直在探索
Stack Overflow data dumps,从而利用友好的XML和“解析”正则表达式。我尝试使用各种Haskell XML库来查找特定用户的文档顺序中的第一个帖子,都是讨厌的。
import Control.Monad import Text.HTML.TagSoup userid = "83805" main = do posts <- liftM parseTags (readFile "posts.xml") print $ head $ map (fromAttrib "Id") $ filter (~== ("<row OwnerUserId=" ++ userid ++ ">")) posts
import Text.XML.HXT.Arrow import Text.XML.HXT.XPath userid = "83805" main = do runX $ readDoc "posts.xml" >>> posts >>> arr head where readDoc = readDocument [ (a_tagsoup,v_1),(a_parse_xml,(a_remove_whitespace,(a_issue_warnings,v_0),(a_trace,v_1) ] posts :: ArrowXml a => a XmlTree String posts = getXPathTrees byUserId >>> getAttrValue "Id" where byUserId = "/posts/row/@OwnerUserId='" ++ userid ++ "'"
import Control.Monad import Control.Monad.Error import Control.Monad.Trans.Maybe import Data.Either import Data.Maybe import Text.XML.Light userid = "83805" main = do [posts,votes] <- forM ["posts","votes"] $ liftM parseXML . readFile . (++ ".xml") let ps = elemNamed "posts" posts putStrLn $ maybe "<not present>" show $ filterElement (byUser userid) ps elemNamed :: String -> [Content] -> Element elemNamed name = head . filter ((==name).qName.elName) . onlyElems byUser :: String -> Element -> Bool byUser id e = maybe False (==id) (findAttr creator e) where creator = QName "OwnerUserId" Nothing Nothing
我哪里做错了?使用Haskell处理大量XML文档的正确方法是什么?
我注意到在所有这些情况下你都在做String IO。您绝对必须使用Data.Text或Data.Bytestring(.Lazy),如果您希望有效地处理大量的文本,String = [Char],这是非常大的平面文件的不适当的表示。
原文链接:https://www.f2er.com/xml/293231.html那就意味着你需要使用支持bytestrings的Haskell XML库。几十个xml库在这里:http://hackage.haskell.org/packages/archive/pkg-list.html#cat:xml
我不知道哪些支持bytestrings,但这是你正在寻找的条件。