Groovy:Xml:如何在gsp页面的textarea中显示Xml响应 [英] Groovy:Xml: How to display Xml response in textarea of gsp page
问题描述
将一个Xml响应存储在一个字符串中,我想将它显示在gsp页面的textarea中。
$ p $ String responseXml = <汽车>
< Car> benz< / Car>
< Car> audi< / Car>
< Car> bmw< / Car>
< / Cars>
在gsp页面中
< g:textArea name =xmlvalue =$ {responseXml}rows =20cols =100/>
在textarea中获得响应,就像这样一行xml标签
pre $< Cars>< Car> benz< / Car>< Car> audi< / Car>< Car> bmw&汽车>
但我想要的是在textarea中显示xml标签,像这样
< Cars>
< Car> benz< / Car>
< Car> audi< / Car>
< Car> bmw< / Car>
< / Cars>
我为此创建了一个taglib,在多个位置:
/ **
*显示为html时,保留所提供值的换行符和空格。
* @param value - 保存换行符的值。
* /
def preserveFormat = {attrs,body - >
def value = attrs.value
out<< value.encodeAsHTML()。replace('\ n','< br /> \\\
')。replace('','& nbsp;')。replace('\'','& amp;' ; & nbsp;& nbsp;')
}
/ **
*以相当格式化的方式显示xml内容并保留格式html视图。
* /
def displayXml = {attrs,body - >
def xml = attrs.xml
assert xml
def prettyXml = groovy.xml.XmlUtil.serialize(xml)
out<< preserveFormat(value:prettyXml)
}
我用一个额外的taglib更新了我的回复,根据下面评论中的建议,以一种漂亮的方式格式化xml。
Have an Xml response stored in a string, i want to display it in a textarea in gsp page
String responseXml = ""<Cars>
<Car>benz</Car>
<Car>audi</Car>
<Car>bmw</Car>
</Cars>""
in gsp page
<g:textArea name="xml" value="${responseXml}" rows="20" cols="100"/>
getting response in textarea as a single line of xml tags like this
<Cars><Car>benz</Car><Car>audi</Car><Car>bmw</Car></Cars>
but what i want is display xml tags in textarea like this
<Cars>
<Car>benz</Car>
<Car>audi</Car>
<Car>bmw</Car>
</Cars>
I created a taglib for this since I had this problem in muitiple places:
/**
* Preserves line breaks and spaces of the supplied value when displaying as html.
* @param value - The value to preserve linebreaks of.
*/
def preserveFormat ={ attrs, body ->
def value = attrs.value
out << value.encodeAsHTML().replace('\n', '<br/>\n').replace(' ',' ').replace('\t',' ')
}
/**
* Displays xml content in a pretty formatted way and preserves formatting in html view.
*/
def displayXml={attrs, body ->
def xml = attrs.xml
assert xml
def prettyXml = groovy.xml.XmlUtil.serialize(xml)
out << preserveFormat(value:prettyXml)
}
I updated my reply with an additional taglib that also formats xml in a pretty way according to suggestion in comments below.
这篇关于Groovy:Xml:如何在gsp页面的textarea中显示Xml响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!