我试图用XPath和箭头同时通过HXT来解决问题,我完全坚持如何思考这个问题.我有以下HTML:
<div>
<div class="c1">a</div>
<div class="c2">b</div>
<div class="c3">123</div>
<div class="c4">234</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我已经提取到HXT XmlTree中了.我想做的是定义一个函数(我想?):
getValues :: [String] -> IOSArrow Xmltree [(String, String)]
Run Code Online (Sandbox Code Playgroud)
哪个,如果用作getValues ["c1", "c2", "c3", "c4"],将得到我:
[("c1", "a"), ("c2", "b"), ("c3", "123"), ("c4", "234")]
Run Code Online (Sandbox Code Playgroud)
请帮忙?
我试图使用HXT从6行的XML输入中提取一些数据.我也希望保留HXT,因为Curl集成,因为我有其他XML文件有数千行,稍后.
我的XML看起来像这样:
<?xml version = "1.0" encoding = "UTF-8"?>
<find>
<set_number>228461</set_number>
<no_records>000000008</no_records>
<no_entries>000000008</no_entries>
</find>
Run Code Online (Sandbox Code Playgroud)
我一直试图聚在一起解析它.不幸的是,HXT的Wiki页面并没有太大的帮助(或者我只是忽略了一些东西).
data FindResult = FindResult {
resultSetNumber :: String,
resultNoRecords :: Int,
resultNoEntries :: Int
} deriving (Eq, Show)
resultParser :: ArrowXml a => a XmlTree FindResult
resultParser = hasName "find" >>> getChildren >>> proc x -> do
setNumber <- isElem >>> hasName "set_number" >>> getChildren >>> getText -< x
noRecords <- isElem >>> hasName "no_records" >>> getChildren >>> getText -< x
noEntries <- isElem >>> …Run Code Online (Sandbox Code Playgroud) 我想从String读取HTML,处理它并使用HXT将更改的文档作为String返回.由于此操作不需要IO,我宁愿执行箭头而runLA不是使用runX.
代码看起来像这样(省略处理以简化):
runLA (hread >>> writeDocumentToString [withOutputHTML, withIndent yes]) html
Run Code Online (Sandbox Code Playgroud)
但是,html结果中缺少周围的标记:
["\n <head>\n <title>Bogus</title>\n </head>\n <body>\n Some trivial bogus text.\n </body>\n",""]
Run Code Online (Sandbox Code Playgroud)
当我使用runX时,就像这样:
runX (readString [] html >>> writeDocumentToString [withOutputHTML, withIndent yes])
Run Code Online (Sandbox Code Playgroud)
我得到了预期的结果:
["<html>\n <head>\n <title>Bogus</title>\n </head>\n <body>\n Some trivial bogus text.\n </body>\n</html>\n"]
Run Code Online (Sandbox Code Playgroud)
为什么会这样,我该如何解决?
我正在尝试在Haskell中学习Arrows,所以我正在使用基于箭头的HXT库编写一个简单的应用程序.HXT wiki和教程中的示例放弃了函数类型签名.但是,我非常喜欢类型,并试图找出如何使用它们.这是我遇到绊脚石的地方.鉴于这些功能:
readXml str = runX (readString [withValidate no] str)
atTag tag = deep (isElem >>> hasName tag)
Run Code Online (Sandbox Code Playgroud)
我认为他们应该被分配以下签名:
readXml ? String ? IO [XmlTree]
atTag ? ArrowXml a ? String ? a XmlTree XmlTree
Run Code Online (Sandbox Code Playgroud)
我试图使用箭头语法将它们连接在一起,如下所示:
parseItem = proc str -> do
desc <- text <<< atTag "description" <<< arr readXml -< str
...
Run Code Online (Sandbox Code Playgroud)
但是,如果我的类型签名是正确的(GHC没有抱怨),我需要一种方法来结合monad语法和箭头语法来获取XmlTree并返回IO.
我不确定如何继续.有人有任何见解吗?
我正在尝试解析以下XML文档HXT:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Key</key>
<string>Value</string>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)
我不想在这里进行任何验证,因为它需要网络访问.不幸的是,HXT仍然希望安装hxt-curl/ hxt-httppackages来解析这个简单的文档:
Prelude> :m +Text.XML.HXT.Core
Prelude Text.XML.HXT.Core> runX $ readDocument [withValidate no] "example.xml"
fatal error: HTTP handler not configured,
please install package hxt-curl and use 'withCurl' config option
or install package hxt-http and use 'withHTTP' config option
Run Code Online (Sandbox Code Playgroud)
我不想将hxt-curl/ hxt-httppackages 添加到依赖项列表中,因为我并不真正需要它们.我无法更改我正在解析的文档.移动到另一个xml解析库也是不可取的.
有没有办法用HXT解析样本文档而不添加不必要的包?
我在弄清楚为什么HXT正在取代我的DTD时遇到了一些麻烦.首先,这是我要解析的输入文件:
<!DOCTYPE html>
<html>
<head>
<title>foo</title>
</head>
<body>
<h1>foo</h1>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
这是我得到的输出:
<?xml version="1.0" encoding="US-ASCII"?>
<html>
<head>
<title>foo</title>
</head>
<body>
<h1>foo</h1>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
最后,这是我正在使用的箭头的简化版本:
start (App src dest) = runX $
readDocument [ withValidate no
, withSubstDTDEntities no
, withParseHTML yes
--, withTagSoup
]
src
>>>
this
>>>
writeDocument [ withIndent yes
, withSubstDTDEntities no
, withOutputHTML
--, withOutputEncoding "UTF-8"
]
dest
Run Code Online (Sandbox Code Playgroud)
我为评论道歉 - 我一直在玩弄不同的配置组合.我似乎无法让HXT 不要乱用DTD,即使是withSubstDTDEntities no,withValidate no等等.我收到警告说HXT忽略了我的doctype声明,但这是我唯一的洞察力.有人可以借给我一个手吗?先感谢您!
我正在解析一个XML文件,HXT我试图将一些节点提取分解为模块化部分(我一直在使用它作为我的指南).不幸的是,一旦我进行了第一级解析,我无法弄清楚如何应用一些选择器.
import Text.XML.HXT.Core
let node tag = multi (hasName tag)
xml <- readFile "test.xml"
let doc = readString [withValidate yes, withParseHTML no, withWarnings no] xml
books <- runX $ doc >>> node "book"
Run Code Online (Sandbox Code Playgroud)
我看到书有类型 [XmlTree]
:t books
books :: [XmlTree]
Run Code Online (Sandbox Code Playgroud)
现在我想得到第一个元素,books然后在子树中提取一些值.
let b = head(books)
runX $ b >>> node "cost"
Couldn't match type ‘Data.Tree.NTree.TypeDefs.NTree’
with ‘IOSLA (XIOState ()) XmlTree’
Expected type: IOSLA (XIOState ()) XmlTree XNode
Actual type: XmlTree
In the first argument …Run Code Online (Sandbox Code Playgroud) 我想解析以下示例XML文件,而没有pickler模块。
<?xml version="1.0" encoding="utf-8" ?>
<Groups>
<Name>ABC</Name>
<GroupA>
<Name>Foo</Name>
<Sum>100</Sum>
</GroupA>
<GroupB>
<Name>Bar</Name>
<Sum>0</Sum>
</GroupB>
</Groups>
Run Code Online (Sandbox Code Playgroud)
我结束了这个:
{-# language Arrows #-}
import Text.XML.HXT.Core
data Groups = Groups GroupA GroupB deriving Show
data GroupA = GroupA String String deriving Show
data GroupB = GroupB String String deriving Show
readGroup :: LA XmlTree Groups
readGroup = deep (isElem >>> hasName "Groups") >>> getChildren >>>
proc root -> do
a <- readGroupA -< root
b <- readGroupB -< root
returnA -< Groups a …Run Code Online (Sandbox Code Playgroud) 我花了很多时间试图弄清楚如何使用HXT.我一直反对使用的例子deep.怎么deep办?
例如,此代码具有以下内容:
atTag tag = deep (isElem >>> hasName tag)
Run Code Online (Sandbox Code Playgroud)
-- case-insensitive tag matching
atTagCase tag = deep (isElem >>> hasNameWith ((== tag') . upper . localPart))
where tag' = upper tag
upper = map toUpper
Run Code Online (Sandbox Code Playgroud) 我正在编写一个小应用程序,旨在从多个站点中提取XML,然后以我想要的方式处理数据.我之前用不同的语言制作了这样的应用程序,我正在为Haskell实践编写这个应用程序.
无论如何,到了这一点.在百万和一个不同的XML解析器环顾网络后,我决定选择HXT,因为谁不喜欢箭头.在页面http://www.haskell.org/haskellwiki/HXT/Conversion_of_Haskell_data_from/to_XML之后,我已经找到了一些似乎读取我的XML文件并将其放入我定义的Haskell数据类型的东西.我正在使用XmlPickler的实例来读取文件中的数据来实现这一目标.除了这个错误,我到达了可以工作的东西:
fatal error: document unpickling failed
xpCheckEmptyAttributes: unprocessed XML attribute(s) detected
Run Code Online (Sandbox Code Playgroud)
我知道我没有处理所有属性.我不想要所有的属性.有没有办法忽略这些?我想我可以处理所有属性,将它们放在一个新的数据类型中,然后从中提取属性以获取我真正想要的数据.我想避免这个小小的黑客,因此我在这里,要求正确的方式™.
我使用错误的工具来完成工作吗?解开第三方数据是不安全的(就像在Python中一样)?
我环顾网络寻找解决方案,但Text.XML.HXT.Arrow.XmlState.SystemConfig似乎没有我需要禁用此行为.
我正在研究我在这里工作的项目:
我在我的函数中使用monad变换器方法看起来像这样:
scrapePost :: String -> IO ()
scrapePost url = liftM (fromMaybe ()) . runMaybeT $ do
doc <- lift $ fromUrl url
-- get a bunch of stuff from the page
-- send it to the db
replies <- lift . runX $ doc >>> css ".post.reply"
-- here is the problem
mapM_ (parseReply url (fromJust page_id)) replies
-- here is the problem
Run Code Online (Sandbox Code Playgroud)
parseReply 是我需要的功能,但我似乎无法做到正确.
这是我开始这个功能的微弱尝试:
parseReply :: String -> String -> XNode -> Maybe ()
parseReply …Run Code Online (Sandbox Code Playgroud) 我正在阅读一个包含文字重音词(西班牙语)的HTML网页:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Web page</title>
<body>
<p>Título</p>
<p>Año</p>
<p>Ángel</p>
<p>¿por qué nos vamos?</p>
</body>
Run Code Online (Sandbox Code Playgroud)
我正在使用HXT:
...
let doc = readDocument [ withValidate no
, withInputEncoding iso8859_1
, withParseHTML yes
, withWarnings no
, withEncodingErrors no
, withCurl []] url
...
Run Code Online (Sandbox Code Playgroud)
使用选项
withInputEncoding utf8
丢弃这些字符,得到以下字样:Ttulo,Ao,ngel,por qunos vamos? 使用选项
withInputEncoding iso8859_1
将这些字符转换为字符串,得到结果如:Rom\225ntica,Man\180s,H\233ctor.其中\ 225,\ 180或\ 233是字符串 …