简体   繁体   English

如何在Groovy MarkupBuilder中禁用漂亮打印?

[英]How to disable pretty-printing in Groovy MarkupBuilder?

I have a string input in my Mule flow. 我的M子流中有一个字符串输入。 It passes through my Groovy Script and outputs XML. 它通过我的Groovy脚本并输出XML。 I originaly had the script followed by an XSLT converter to remove empty nodes and set the indent to "no" in the output tag. 我原来是脚本,然后是XSLT转换器,以删除空节点并将缩进量设置为输出标签中的“ no”。 But now I removed it as I cannot use it in conjunction with my script if I want to keep the special characters (see previous question here ). 但是现在我删除了它,因为如果我想保留特殊字符,则无法将其与脚本结合使用(请参见此处的上一个问题)。

Instead I now check each value before printing the nodes. 现在,我现在在打印节点之前检查每个值。 But the problem I have is my XML needs to be unindented in order to work with my InDesign project I adapt the XML for. 但是我的问题是我的XML需要缩进才能与适应XML的InDesign项目一起使用。 I lost that ability when I removed the XSLT so I fixed one problem but created another. 当我删除XSLT时,我失去了这种能力,所以我解决了一个问题,却创建了另一个问题。

I found the method getPrinter(), I used it with the setAutoIndent(false) but it didn't change anything to the output and created no errors. 我找到了方法getPrinter(),将它与setAutoIndent(false)一起使用,但它没有对输出进行任何更改,也没有产生任何错误。 Not to sure where to use it. 不知道在哪里使用它。

Here's my script : 这是我的脚本:

public Boolean isEmpty(value){
    if(value.toString().trim() == "" || value.toString().trim() == '' || value == null)
        return true;
}

root = new XmlSlurper(false,false).parseText(payload)

if(root.name() == 'GetActivitiesResponse')
    startEach = root.children().children()
else
    startEach = root.children()

def xml = new StringWriter().with { w -> new groovy.xml.MarkupBuilder(w).with {
        mkp.xmlDeclaration(version: "1.0", encoding: "utf-8")
                 escapeAttributes = false  
                 getPrinter().setAutoIndent(false);  
                 "w_import_saisie_web"() {
                    startEach.each { p -> "w_evenement"() {
                        if(!isEmpty(p.PresentationDate))
                            "w_dates"{ mkp.yieldUnescaped (p.PresentationDate.toString() + "
")  }
                        if(!isEmpty(p.SubTitle))                                
                            "w_contexte"{ mkp.yieldUnescaped (p.SubTitle.toString() + "
") }
                        //if(!isEmpty(p.SubTitle)) 
                            "w_nom_evenement"{ /*p.GEVT_Type*/ mkp.yieldUnescaped ("Nom evenement" + "
") }
                        if(!isEmpty(p.Name)) 
                            "w_titre"{ mkp.yieldUnescaped (p.Name.toString() + "
")}
                        if(!isEmpty(p.ShortDescription) || !isEmpty(p.Teaser))      
                            "w_texte"{mkp.yieldUnescaped (p.ShortDescription.toString() +  p.Teaser.toString() + "
")}

                             p.SubEvents.children().each { q -> "w_bloc_sous_evenement"() {
                                if(!isEmpty(q.PresentationDate) || !isEmpty(q.Name)) 
                                    "w_sous_eve_titre"{ mkp.yieldUnescaped (q.PresentationDate.toString() + q.Name.toString() + "
")}
                                if(!isEmpty(q.ShortDescription) || !isEmpty(q.Teaser) || !isEmpty(q.WebDescription))  
                                    "w_sous_eve_desc"{mkp.yieldUnescaped (q.ShortDescription.toString() + q.Teaser.toString() + q.WebDescription.toString() + "
")}
                                }
                             }   
                             if(!isEmpty(p.Site) || !isEmpty(p.PresentationHours))
                                "w_coordonnees"{ mkp.yieldUnescaped ("teeeessdfsdfsdfst" +  p.Site.toString() + ' - ' + p.PresentationHours.toString() + "
")}                
                        }
                    }
                }
    }
    w.toString()
}

Add an IndentPrinter when you create the MarkupBuilder. 创建MarkupBuilder时添加一个IndentPrinter。

def xml = new MarkupBuilder(new IndentPrinter(new PrintWriter(writer), "", true))

See this question: groovy.xml.MarkupBuilder disable PrettyPrint 看到这个问题: groovy.xml.MarkupBuilder disable PrettyPrint

I tried a bunch of different things to see if setAutoIndent was effective (setting it before passing the IndentPrinter to the MarkupBuilder for example) and it didn't seem to have any effect. 我尝试了一堆不同的方法来查看setAutoIndent是否有效(例如,在将IndentPrinter传递给MarkupBuilder之前进行设置),但似乎没有任何效果。 So, like you, I'm wondering about its purpose. 因此,像您一样,我想知道它的目的。

意识到我太费劲了...只是在末尾的toString()中添加了这条简单的线...

w.toString().replaceAll(">\\s+<", "><").trim();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM