Ugly xml output

I use HaXml to convert an XML file, and it all works great. However, the output of HaXml generates a very ugly appearance, mainly because it inserts a line into almost every closing bracket. Here is the code that xml generates:

writeFile outPath (show . PP.content . head $ test (docContent (posInNewCxt "" Nothing) (xmlParse "" "")))

test = 
    mkElemAttr "test" [("a", literal "1"), ("b", literal "2")]
        [
            mkElem "nested" []
        ]

and here is the result:

<test a="1" b="2"
  ><nested/></test>

Of course, this is worse with a lot of elements.

I know that HaXml uses Text.PrettyPrint.HughesPJ for rendering, but uses different styles has not changed much.

So, is there a way to change the output?

+3
source share
1 answer

Replacing the call with showwith Text.PrettyPrint.renderStyle, you can get several different types of behavior:

import Text.XML.HaXml
import Text.XML.HaXml.Util
import Text.XML.HaXml.Posn
import qualified Text.XML.HaXml.Pretty as PP
import Text.PrettyPrint

main = writeFile "/tmp/x.xml" (renderStyle s . PP.content 
                                             . head $
               test (docContent (posInNewCxt "" Nothing) (xmlParse "" "")))
    where
        s = style { mode = LeftMode, lineLength = 2 }

test = 
    mkElemAttr "test" [("a", literal "1"), ("b", literal "2")]
        [
            mkElem "nested" []
        ]

Experimenting with different styles out of the box:

Default style

<test a="1" b="2"
  ><nested/></test>

style {mode = OneLineMode}

<test a="1" b="2" ><nested/></test>

style {mode = LeftMode, lineLength = 2}

<test a="1"
b="2"
><nested/></test>

, .

, , fullRender:

fullRender
    :: Mode                     -- Rendering mode
    -> Int                      -- Line length
    -> Float                    -- Ribbons per line
    -> (TextDetails -> a -> a)  -- What to do with text
    -> a                        -- What to do at the end
    -> Doc                      -- The document
    -> a                        -- Result

TextDetails.

+3

All Articles