在Groovy MarkupBuilder中使用命名空间 [英] Use of Namespaces in Groovy MarkupBuilder
问题描述
<?xml version =1.0encoding =UTF-8 ?>
< structure:structuralDataRoot xmlns:register =http://www.test.ch/register/1xmlns:structure =http://test.ch/structure/1>
< structure:tester> ZH< / structure:tester>
< structure:surveyYear> 2001< / structure:surveyYear>
< structure:surfaceData>
< structure:houseSurfaceData>
< structure:creationDate> 2001-01-01< / structure:creationDate>
< structure:localFarmId>
< register:houseIdCategory>标记< / register:houseIdCategory>
< register:houseId>令牌< / register:houseId>
< / structure:localFarmId>
< / structure:houseSurfaceData>
< / structure>
我可以将命名空间添加到xml中,如下所示:
xml.records('xmlns:structure':http://test.ch/structure/1...
但是我如何为xml元素创建一个名称空间前缀?
我找到的唯一解决方案是:
tester('xmlns:structure':http://test.ch/structure/1,'ZH')
但是,这给了我以下输出:
< tester xmlns:structure ='http://test.ch/structure/1'> ZH< / tester>
它在语法上是正确的,但是当你有很多节点时不好阅读。
你可以做到这一点(不知道它是你想要的)虽然你想要什么,但 这段代码输出: I want have the following output: I can add namespace to an xml like this: But how I can make a namespace prefix to an xml-element?
The only solution I found was this: But this gives me the follwing output: It's syntactical correct, but not nice to read when you have many nodes. You can do this (not sure it's what you want though) That code outputs:
这篇关于在Groovy MarkupBuilder中使用命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
$ b $ pre $ import groovy.xml.StreamingMarkupBuilder
import groovy .xml.XmlUtil
$ b $ def xmlBuilder = new StreamingMarkupBuilder()
writer = xmlBuilder.bind {
mkp.decla reNamespace(register:http://www.test.ch/register/1)
mkp.declareNamespace(结构:http://test.ch/structure/1)
'结构:structureDataRoot'{
'structure'tester'('ZH')
'structure:surveyYear'(2001)
'structure:surfaceData'{
'structure:houseSurfaceData'{
'structure:creationDate'('2001-01-01')
'结构:localFarmId'{
'register:houseIdCategory'('token')
'register:houseId '('token')
}
}
}
}
}
println XmlUtil.serialize(writer)
<?xml version =1.0encoding =UTF-8?>
< structure:structuralDataRoot xmlns:register =http://www.test.ch/register/1xmlns:structure =http://test.ch/structure/1>
< structure:tester> ZH< / structure:tester>
< structure:surveyYear> 2001< / structure:surveyYear>
< structure:surfaceData>
< structure:houseSurfaceData>
< structure:creationDate> 2001-01-01< / structure:creationDate>
< structure:localFarmId>
< register:houseIdCategory>标记< / register:houseIdCategory>
< register:houseId>令牌< / register:houseId>
< / structure:localFarmId>
< / structure:houseSurfaceData>
< / structure:surfaceData>
< / structure:structuralDataRoot>
<?xml version="1.0" encoding="UTF-8"?>
<structure:structuralDataRoot xmlns:register="http://www.test.ch/register/1" xmlns:structure="http://test.ch/structure/1" >
<structure:tester>ZH</structure:tester>
<structure:surveyYear>2001</structure:surveyYear>
<structure:surfaceData>
<structure:houseSurfaceData>
<structure:creationDate>2001-01-01</structure:creationDate>
<structure:localFarmId>
<register:houseIdCategory>token</register:houseIdCategory>
<register:houseId>token</register:houseId>
</structure:localFarmId>
</structure:houseSurfaceData>
</structure>
xml.records('xmlns:structure' :"http://test.ch/structure/1" ...
tester('xmlns:structure' :"http://test.ch/structure/1", 'ZH')
<tester xmlns:structure='http://test.ch/structure/1'>ZH</tester>
import groovy.xml.StreamingMarkupBuilder
import groovy.xml.XmlUtil
def xmlBuilder = new StreamingMarkupBuilder()
writer = xmlBuilder.bind {
mkp.declareNamespace( register: "http://www.test.ch/register/1" )
mkp.declareNamespace( structure: "http://test.ch/structure/1" )
'structure:structuralDataRoot' {
'structure:tester'( 'ZH' )
'structure:surveyYear'( 2001 )
'structure:surfaceData' {
'structure:houseSurfaceData' {
'structure:creationDate'( '2001-01-01' )
'structure:localFarmId' {
'register:houseIdCategory'( 'token' )
'register:houseId'( 'token' )
}
}
}
}
}
println XmlUtil.serialize( writer )
<?xml version="1.0" encoding="UTF-8"?>
<structure:structuralDataRoot xmlns:register="http://www.test.ch/register/1" xmlns:structure="http://test.ch/structure/1">
<structure:tester>ZH</structure:tester>
<structure:surveyYear>2001</structure:surveyYear>
<structure:surfaceData>
<structure:houseSurfaceData>
<structure:creationDate>2001-01-01</structure:creationDate>
<structure:localFarmId>
<register:houseIdCategory>token</register:houseIdCategory>
<register:houseId>token</register:houseId>
</structure:localFarmId>
</structure:houseSurfaceData>
</structure:surfaceData>
</structure:structuralDataRoot>