2013-06-22 6 views
1

xml 파일의 정보를 조회 테이블로 가져 오려고합니다. 지금까지 나는 어떤 librairies를 사용할 수 있고 어떻게 사용할 수 있는지 읽고있었습니다. 나는 hxt와 hashtables로 갔다. 나는 많은 예제를 보았다
xml을 해시 테이블에 넣기

-- | 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 

...
nametest1, TEST1
nametest2, TEST2
등 : 나는 다음과 같은 쌍을하고 싶은

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

<tables> 

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

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

</tables> 

: 여기 파일입니다 유사한 작업을 수행하는 방법에 대해서는 알고 있지만 각 노드에서 이름 속성을 얻는 방법을 이해할 수는 없습니다.

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 

의 건설 :

건배 rakwatt 여기

답변

1

쌍 test.xml의 이름을 가진 파일을 판독하고, 막 (이름, 텍스트)를 출력한다 일례이다 쌍은 getAttrAndText에서 발생합니다. 나머지 함수는 파일을 열고 태그의 하위 태그 인 모든 태그를 선택합니다. 여전히 텍스트의 공백을 제거 할 수 있습니다.

+0

대단히 감사합니다. – rakwatt

관련 문제