<?xml version =1.0encoding =UTF-8 ?> < structure:structuralDataRoot xmlns:register =www.test.ch/register/1xmlns:structure =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':test.ch/structure/1...但是我如何为xml元素创建一个名称空间前缀?我找到的唯一解决方案是:
tester('xmlns:structure':test.ch/structure/1,'ZH')但是,这给了我以下输出:
< tester xmlns:structure ='http://test.ch/structure/1'> ZH< / tester>它在语法上是正确的,但是当你有很多节点时不好阅读。
解决方案你可以做到这一点(不知道它是你想要的)虽然你想要什么,但 $ b $ pre $ import groovy.xml.StreamingMarkupBuilder import groovy .xml.XmlUtil $ b $ def xmlBuilder = new StreamingMarkupBuilder() writer = xmlBuilder.bind { mkp.decla reNamespace(register:www.test.ch/register/1) mkp.declareNamespace(结构: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 =www.test.ch/register/1xmlns:structure =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>
I want have the following output:
<?xml version="1.0" encoding="UTF-8"?> <structure:structuralDataRoot xmlns:register="www.test.ch/register/1" xmlns:structure="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>I can add namespace to an xml like this:
xml.records('xmlns:structure' :"test.ch/structure/1" ...But how I can make a namespace prefix to an xml-element? The only solution I found was this:
tester('xmlns:structure' :"test.ch/structure/1", 'ZH')But this gives me the follwing output:
<tester xmlns:structure='test.ch/structure/1'>ZH</tester>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)
import groovy.xml.StreamingMarkupBuilder import groovy.xml.XmlUtil def xmlBuilder = new StreamingMarkupBuilder() writer = xmlBuilder.bind { mkp.declareNamespace( register: "www.test.ch/register/1" ) mkp.declareNamespace( structure: "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 )That code outputs:
<?xml version="1.0" encoding="UTF-8"?> <structure:structuralDataRoot xmlns:register="www.test.ch/register/1" xmlns:structure="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>
更多推荐
在Groovy MarkupBuilder中使用命名空间
发布评论