xmlhaskellxml-parsing

Convert the modified cursors back to a document


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?


Solution

  • 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 and inverted what it did. Two notes:

    1. It needs the original Document because fromDocument discards the prologue and epilogue.
    2. It returns its result in a 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.