xml - 如何对名称中包含连字符的属性使用 groovy XMLbuilder

标签 xml groovy

我正在使用 groovy 和 groovy.xml.MarkupBuilder 编写一个库,我将用它来自动创建 epub 文件。我正在编写一个可用于生成 container.xml 文件的函数,我注意到当我尝试使用带有连字符的属性名称时,我的 IDE 会给出一个错误,而当属性名称没有连字符时,我会发现这个错误连字符。

def writer = new StringWriter();
def xml = new MarkupBuilder(writer);
def fullPath="full-path"
def mediaType="media-type"


def generateContainer()
{
    xml.xmlDeclaration()
    xml.container(version:'1.0', xmlns:'urn:oasis:names:tc:opendocument:xmlns:container')
    {
        rootfiles
        {
            rootfile( this.fullPath:'OEBPS/content.opf',this.mediaType:'application/oebps-package+xml')
        }
    }
}

当我尝试使用

full-path

我收到一个错误。当我尝试使用

fullpath

它不会给我错误。

为什么会发生这种情况以及如何纠正它?请参阅here

The value of full-path (in bold) is the only part of this file that will ever vary. 

我希望尽可能准确

最佳答案

这按预期工作:

import groovy.xml.MarkupBuilder

def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
def fullPath = "full-path"
def mediaType = "media-type"

xml.mkp.xmlDeclaration( version: "1.0", encoding: "utf-8" )    
xml.container( version:'1.0', 
               xmlns:'urn:oasis:names:tc:opendocument:xmlns:container' ) {

    rootfiles {
        rootfile( (fullPath) : 'OEBPS/content.opf', 
                  (mediaType): 'application/oebps-package+xml' )
    }
}

println writer

确保在引用变量 fullPath 时,必须使用 (fullPath) [大括号],以便将变量的值用作属性xml 节点。

关于xml - 如何对名称中包含连字符的属性使用 groovy XMLbuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23207234/

相关文章:

grails - 在 grails 中编写约束参数的首选方式

java - 将 POJO 转换为 XML

java 从 .txt 文件更新 .XML 文件

java - 有没有一种简单的方法可以将按钮添加到子 Activity 的操作栏?

testing - SoapUI Free - 用于获取 TestSuite 属性的属性值中的 Groovy 脚本

java - 一对多关系。从数据存储中选择对象

groovy - 如何在groovy中动态创建方法名称

java - 在 groovy 中应用一些谓词到流过滤器

android - 如何在 xml 中的 Ternaroy 运算符内使用三元条件,同时使用 android 数据绑定(bind)将数据设置到 TextView

xml - 实现 W3C XML DOM 的 Node.js 库?