I want to use the haskell module Text.XML.Cursor
to parse a xml document.
First I convert the document to cursors with fromDocument
, then apply some filter. Now I want to get the document back. I think there should be a toDocument
function, but it appears no. How can I Convert the modified cursors back?
Here's a filterDocument
function that should do what you want:
import Text.XML (Document(..), Node(..))
import Text.XML.Cursor (Cursor, fromDocument, node)
filterDocument :: (Cursor -> Cursor) -> Document -> Maybe Document
filterDocument f doc = case node (f (fromDocument doc)) of
NodeElement root -> Just doc{ documentRoot = root }
_ -> Nothing
I basically just looked at the definition of the fromDocument
function and inverted what it did. Two notes:
Document
because fromDocument
discards the prologue and epilogue.Maybe
because the Cursor
you return might point to a non-element node (e.g., a comment or text node), but an XML document needs an element as the root.