在Groovy MarkupBuilder中使用命名空间

编程入门 行业动态 更新时间:2024-10-24 22:29:03
本文介绍了在Groovy MarkupBuilder中使用命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

<?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中使用命名空间

本文发布于:2023-10-23 06:05:46,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:空间   Groovy   MarkupBuilder

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!