abcdeef
abcdeef

Reputation: 21

Haskell HXT parse condition

i'm trying to parse this xml:

...
<member>
  <name>id</name>
  <value>
    <string>1</string>
  </value>
</member>
<member>
  <name>description</name>
  <value>
    <string>sdfsdfsdf</string>
  </value>
</member>
...

how to parse only the "<member>" tags with a subordinate "name"-tag = "id"?

i tried:

getroot = multi ( ( getChildren >>> hasName "name" >>> hasText "id") `guards` (isElem >>> hasName "member"  ) ) 

main = do
print <- runX (parseXML "test2.xml" >>> getroot >>> putXmlTree "-")

Upvotes: 2

Views: 277

Answers (1)

Joachim Breitner
Joachim Breitner

Reputation: 25763

When you use the filter hasName "name", you get the <name> tag. That node itself is not a text node, so hasText "id" fails. Here a modification that seems to work: (I also had to change the type of the argument of hasText, maybe a different version of HXT)

import Text.XML.HXT.Core

getroot = multi ( ( getChildren >>> hasName "name" >>> getChildren >>> hasText (=="id")) 
                   `guards` (isElem >>> hasName "member"  ) ) 

main = do
    runX (readDocument [] "test2.xml" >>> getroot >>> putXmlTree "-")

I’m not an expert of HXT so it might be that there is a much better way to do what you want.

Upvotes: 1

Related Questions