SVG定义了一个名为use
的元素,我试图使用groovy.xml.MarkupBuilder
来利用此标记来生成SVG文件:
http://tutorials.jenkov.com/svg/defs-element.html
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
xml.svg {
defs {
g(id:"shape") {
rect(x:50, y:50, width:50, height:50)
circle(cx:50, cy:50, r:50)
}
}
use("xlink:href":"#shape", x:50, y:50")
}
但是use
也是groovy中的关键字。如何正确逃脱?
答案 0 :(得分:2)
不确定这些方法是否是官方的,但它们是否有效:
v1:
def xml = new groovy.xml.MarkupBuilder()
xml.svg {
defs {
g(id:"shape") {
rect(x:50, y:50, width:50, height:50)
circle(cx:50, cy:50, r:50)
}
}
createNode('use',["xlink:href":"#shape", x:50, y:50])
//nested elements could be here
nodeCompleted('svg','use')
}
v2:
def xml = new groovy.xml.MarkupBuilder()
xml.svg {
defs {
g(id:"shape") {
rect(x:50, y:50, width:50, height:50)
circle(cx:50, cy:50, r:50)
}
}
doInvokeMethod('use','use',[["xlink:href":"#shape", x:50, y:50], {
/*nested elements could be here*/
} ])
}
v3:
我们可以重新定义负责名称映射或检查转义规则的getName
方法。
@groovy.transform.CompileStatic
class MyMarkupBuilder extends groovy.xml.MarkupBuilder{
def getName(String name){
if(name.startsWith('__'))return name.substring(2)
return name
}
}
def xml = new MyMarkupBuilder()
xml.svg {
defs {
g(id:"shape") {
rect(x:50, y:50, width:50, height:50)
circle(cx:50, cy:50, r:50)
}
}
__use("xlink:href":"#shape", x:50, y:50)
}