我正在使用 HaXml 转换 XML 文件,一切都运行良好。然而,HaXml 生成的输出看起来非常难看,主要是因为它几乎在每个右括号处插入了一个换行符。 下面是一些生成 xml 的代码:
writeFile outPath (show . PP.content . head $ test (docContent (posInNewCxt "" Nothing) (xmlParse "" "")))
test =
mkElemAttr "test" [("a", literal "1"), ("b", literal "2")]
[
mkElem "nested" []
]
这是它生成的输出:
<test a="1" b="2"
><nested/></test>
当然是元素越多越差。
我知道 HaXml 使用 Text.PrettyPrint.HughesPJ 进行渲染,但使用不同的 styles 变化不大。
那么,有没有办法改变输出?
最佳答案
用 Text.PrettyPrint.renderStyle
代替您调用 show
你可以得到一些不同的行为:
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" []
]
尝试不同的现成样式:
默认样式
<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
函数中。
关于xml - 丑陋的 xml 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6089585/