在Groovy MarkupBuilder中使用命名空间 [英] Use of Namespaces in Groovy MarkupBuilder

查看:307
本文介绍了在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> 

它在语法上是正确的,但是当你有很多节点时不好阅读。

解决方案

你可以做到这一点(不知道它是你想要的)虽然你想要什么,但
$ 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>


I want have the following output:

<?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>

I can add namespace to an xml like this:

xml.records('xmlns:structure' :"http://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' :"http://test.ch/structure/1", 'ZH')

But this gives me the follwing output:

<tester xmlns:structure='http://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: "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 )

That code outputs:

<?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>

这篇关于在Groovy MarkupBuilder中使用命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆