Haskell 将xml放入哈希表
我试图将xml文件中的信息输入到查找表中。 到目前为止,我一直在阅读Libraries可能提供的内容以及如何使用它们。 我使用hxt和哈希表。 文件如下:Haskell 将xml放入哈希表,haskell,xml-parsing,hashtable,Haskell,Xml Parsing,Hashtable,我试图将xml文件中的信息输入到查找表中。 到目前为止,我一直在阅读Libraries可能提供的内容以及如何使用它们。 我使用hxt和哈希表。 文件如下: <?xml version="1.0" encoding="UTF-8" ?> <tables> <table name="nametest1"> test1 </table> <table name="nametest2"> test2 <
<?xml version="1.0" encoding="UTF-8" ?>
<tables>
<table name="nametest1">
test1
</table>
<table name="nametest2">
test2
</table>
</tables>
测试1
测试2
我想要以下几双:名称test1,test1
名称test2,test2
等等……
-- | We get the xml into a hash
getTables :: IO (H.HashTable String String)
getTables = do
confPath <- getEnv "ENCODINGS_XML_PATH"
doc <- runX $ readDocument [withValidate no] confPath
-- this is the part I don't have
-- I get the whole hashtable create and insert process
-- It is the get the xml info that is blocking
where -- I think I might use the following so I shamelessly took them from the net
atTag tag = deep (isElem >>> hasName tag)
text = getChildren >>> getText
——|我们将xml放入哈希中
getTables::IO(H.HashTable字符串)
getTables=do
confPath下面是一个示例,它读取一个名为test.xml的文件,然后只打印出(名称、文本)对:
import Text.XML.HXT.Core
--|成对获取名称属性和所选项目的内容
getAttrAndText::(箭头XML a)=>XmlTree(字符串,字符串)
getAttrAndText=
getAttrValue“name”--并将其和属性名压缩在一起
&&&deep getText——获取节点的文本
--|获取根表项下的所有“表”项
getTableItem::(箭头XML a)=>一个XmlTree XmlTree
可获取项=
deep(hasName“tables”)--在文档中的任意位置查找标记
>>>获取该标记的所有子项
>>>hasName“table”--筛选那些具有标记的
>>>hasAttr“name”--筛选具有属性名称的属性
--|主要功能
main=(print=>getTableItem——获取所有表项
>>>getAttrAndText——获取属性“name”和这些节点的文本
成对的构造在getAttrAndText中进行。其余的函数只需打开文件并选择作为标记直接子项的所有标记。您可能仍然希望去除文本中的前导空格。以下是一个示例,该示例读取名为test.xml的文件,并打印出(名称,文本)对:
import Text.XML.HXT.Core
--|成对获取名称属性和所选项目的内容
getAttrAndText::(箭头XML a)=>XmlTree(字符串,字符串)
getAttrAndText=
getAttrValue“name”--并将其和属性名压缩在一起
&&&deep getText——获取节点的文本
--|获取根表项下的所有“表”项
getTableItem::(箭头XML a)=>一个XmlTree XmlTree
可获取项=
deep(hasName“tables”)--在文档中的任意位置查找标记
>>>获取该标记的所有子项
>>>hasName“table”--筛选那些具有标记的
>>>hasAttr“name”--筛选具有属性名称的属性
--|主要功能
main=(print=>getTableItem——获取所有表项
>>>getAttrAndText——获取属性“name”和这些节点的文本
对的构造在getAttrAndText中进行。其余函数只需打开文件并选择作为标记直接子项的所有标记。您可能仍希望去除文本中的前导空格
import Text.XML.HXT.Core
-- | Gets the name attribute and the content of the selected items as a pair
getAttrAndText :: (ArrowXml a) => a XmlTree (String, String)
getAttrAndText =
getAttrValue "name" -- And zip it together with the the attribute name
&&& deep getText -- Get the text of the node
-- | Gets all "table" items under a root tables item
getTableItem :: (ArrowXml a) => a XmlTree XmlTree
getTableItem =
deep (hasName "tables") -- Find a tag <tables> anywhere in the document
>>> getChildren -- Get all children of that tag
>>> hasName "table" -- Filter those that have the tag <table>
>>> hasAttr "name" -- Filter those that have an attribute name
-- | The main function
main = (print =<<) $ runX $ -- Print the result
readDocument [withValidate no] "test.xml" -- Read the document
>>> getTableItem -- Get all table items
>>> getAttrAndText -- Get the attribute 'name' and the text of those nodes