[英]How to disable pretty-printing in Groovy MarkupBuilder?
我的M子流中有一個字符串輸入。 它通過我的Groovy腳本並輸出XML。 我原來是腳本,然后是XSLT轉換器,以刪除空節點並將縮進量設置為輸出標簽中的“ no”。 但是現在我刪除了它,因為如果我想保留特殊字符,則無法將其與腳本結合使用(請參見此處的上一個問題)。
現在,我現在在打印節點之前檢查每個值。 但是我的問題是我的XML需要縮進才能與適應XML的InDesign項目一起使用。 當我刪除XSLT時,我失去了這種能力,所以我解決了一個問題,卻創建了另一個問題。
我找到了方法getPrinter(),將它與setAutoIndent(false)一起使用,但它沒有對輸出進行任何更改,也沒有產生任何錯誤。 不知道在哪里使用它。
這是我的腳本:
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()
}
創建MarkupBuilder時添加一個IndentPrinter。
def xml = new MarkupBuilder(new IndentPrinter(new PrintWriter(writer), "", true))
看到這個問題: groovy.xml.MarkupBuilder disable PrettyPrint
我嘗試了一堆不同的方法來查看setAutoIndent是否有效(例如,在將IndentPrinter傳遞給MarkupBuilder之前進行設置),但似乎沒有任何效果。 因此,像您一樣,我想知道它的目的。
意識到我太費勁了...只是在末尾的toString()中添加了這條簡單的線...
w.toString().replaceAll(">\\s+<", "><").trim();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.