2013-06-22 99 views
1

我想將xml文件中的信息導出到查找表中。 到目前爲止,我一直在閱讀什麼樣的館藏可用,以及如何使用它們。 我用hxt和hashtables去了。 以下是文件:將xml放入散列表

<?xml version="1.0" encoding="UTF-8" ?> 

<tables> 

    <table name="nametest1"> 
    test1 
    </table> 

    <table name="nametest2"> 
    test2 
    </table> 

</tables> 

我想有以下對:
nametest1,test1的
nametest2,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 

我看到很多例子如何做類似的事情,但我不知道如何獲得每個節點的名稱屬性。

乾杯, rakwatt

回答

1

這裏是讀取用的test.xml的文件名的文件和剛剛打印出的(名字,文本)對一個例子:

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 

的建設對在getAttrAndText中發生。其餘的功能只是打開文件並選擇所有標籤的直接子標籤。您仍然可能想要刪除文本中的前導空白。

+0

非常感謝。 – rakwatt