Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
扩展到多行的xml元素_Xml_Groovy - Fatal编程技术网

扩展到多行的xml元素

扩展到多行的xml元素,xml,groovy,Xml,Groovy,我有一个如下所示的Xml文件:(Xml的一部分) 绑定是一个包含所有需要注入xml的值的映射 <timestamp> ${timestamp} </timestamp> <registered> true </registered> <Person> <name> ${name} </name>

我有一个如下所示的Xml文件:(Xml的一部分)

绑定是一个包含所有需要注入xml的值的映射

<timestamp>
    ${timestamp}
</timestamp>
    <registered>
         true
    </registered>
    <Person>
        <name>
            ${name}
        </name>
        <age>
            ${age}
        </age>
        <height>
            ${height}
        </height>
    </person> 

${timestamp}
真的
${name}
${age}
${height}
值按预期注入,但元素在多行中展开。在使用xmlTemplateEngine处理xml文件后,有没有办法将其恢复为初始格式

我尝试使用XmlUtils.serialize()打印它,但结果是一样的


编辑::由于我没有找到任何方法来保留文件的原始格式,我最终使用了SimpleTemplateEngine。正如daggett所提到的,使用与

相同的结果似乎无法使用XmlTemplateEngine保持原始格式

如果不需要xml验证和XmlTemplateEngine的其他功能,请尝试改用GStringTemplateEngine

它保持原始格式

def tpl='''
<root>
<timestamp>${timestamp}</timestamp>
    <registered>true</registered>
    <Person>
        <name>${name}</name>
        <age>${age}</age>
        <height>${height}</height>
    </Person>
</root>'''

def engine = new groovy.text.GStringTemplateEngine()
def template = engine.createTemplate(tpl).make([
    timestamp: new Date(),
    name: 'mama & mia',
    age: 12,
    height: 175
])
def tpl=''
${timestamp}
真的
${name}
${age}
${height}
'''
def engine=new groovy.text.GStringTemplateEngine()
def template=engine.createTemplate(tpl).make([
时间戳:新日期(),
名字:“妈妈和米娅”,
年龄:12岁,
身高:175
])

似乎无法使用XmlTemplateEngine保持原始格式

如果不需要xml验证和XmlTemplateEngine的其他功能,请尝试改用GStringTemplateEngine

它保持原始格式

def tpl='''
<root>
<timestamp>${timestamp}</timestamp>
    <registered>true</registered>
    <Person>
        <name>${name}</name>
        <age>${age}</age>
        <height>${height}</height>
    </Person>
</root>'''

def engine = new groovy.text.GStringTemplateEngine()
def template = engine.createTemplate(tpl).make([
    timestamp: new Date(),
    name: 'mama & mia',
    age: 12,
    height: 175
])
def tpl=''
${timestamp}
真的
${name}
${age}
${height}
'''
def engine=new groovy.text.GStringTemplateEngine()
def template=engine.createTemplate(tpl).make([
时间戳:新日期(),
名字:“妈妈和米娅”,
年龄:12岁,
身高:175
])

我真的需要使用templateEngine附带的。也许我只是用XmlParser解析文件并添加删除更新节点。感谢dagget。在GStringTemplateEngine中,您可以使用类似jsp的标记
代替和
代替表达式。哦,那太好了。今天晚些时候我会做这件事,然后会回来汇报。我真的需要使用templateEngine附带的工具。也许我只是用XmlParser解析文件并添加删除更新节点。感谢dagget。在GStringTemplateEngine中,您可以使用类似jsp的标记
代替和
代替表达式。哦,那太好了。我们将在今天晚些时候对此进行研究,并将向您汇报。
def tpl='''
<root>
<timestamp>${timestamp}</timestamp>
    <registered>true</registered>
    <Person>
        <name>${name}</name>
        <age>${age}</age>
        <height>${height}</height>
    </Person>
</root>'''

def engine = new groovy.text.GStringTemplateEngine()
def template = engine.createTemplate(tpl).make([
    timestamp: new Date(),
    name: 'mama & mia',
    age: 12,
    height: 175
])