开发者

Ugly xml output

I'm using HaXml to transform a XML file and it's all quite working nicely. However the output HaXml generates looks really ugly, mainly because it inserts a linebreak at almost every closing bracket. Here's some code that generates a xml:

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

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

and here's the output it generates:

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

Of course it is worse with more elements.

I know that HaXml uses Text.PrettyPrint.Hugh开发者_如何学PythonesPJ for rendering, but using different styles didn't change much.

So, is there a way to change the output?


Replacing you call to show with Text.PrettyPrint.renderStyle you can get a few different behaviors:

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 out-of-the-box styles:

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>

So you can certainly do a few different things.

If you don't like any of these, you can write a custom processors, using 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

where your custom behavior can be programmed into the TextDetails function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜