code_money_guji's Blog

Happy coding

Groovy Process Xml(创建XML)

code_money_guji posted @ 2011年4月20日 19:38 in Groovy , 13178 阅读

参考资料:

               http://groovy.codehaus.org/Creating+XML+using+Groovy%27s+MarkupBuilder

         NOTE: 这是笔者的备忘录BLog,如果代码有运行不成功的, 请访问上面给出的链接. 感谢Groovy社区的所有Groovy Monkey 谢谢!

Groovy处理XML之创建XML:

  一 简单的创建实例:

   

def stringWriter = new StringWriter();  
def xml = new MarkupBuilder(stringWriter);
//红色表示元素, 绿色表示属性, 黄色表示元素的text值
xml.cars() {
	   car(name:"car1",no:"1") {
		     country('china')
	                  city('kunming')
		   }
	   
	   car(name:"car2",no:"2") {
		   country('china')
		   city('kunming')
		 }
	   
	   car(name:"car3",no:"3") {
		   country('china')
		   city('kunming')
		 }
	
	}

println stringWriter 
//打印结果如下:
<cars>
  <car name='car1' no='1'>
    <country>china</country>
    <city>kunming</city>
  </car>
  <car name='car2' no='2'>
    <country>china</country>
    <city>kunming</city>
  </car>
  <car name='car3' no='3'>
    <country>china</country>
    <city>kunming</city>
  </car>
</cars>

对比示例程序和输出的结果看出:
 1 一个方法表示一个Element;
 2 方法中的参数代表此ELement的Attributes[propertyName:propertyValue]
 3 方法中如果propertyName 和propertyValue不是成对出现,只是出现了propertyValue,那么将会被设置为ELement的text值.

二 使用DomToGroovy类,将一个现有的XML转化Groovy MakeUpBuilder的相关代码:
class XmlExamples {
	static def CAR_RECORDS = '''
	  <records>
		<car name='HSV Maloo' make='Holden' year='2006'>
		  <country>Australia</country>
		  <record type='speed'>Production Pickup Truck with speed of 271kph</record>
		</car>
		<car name='P50' make='Peel' year='1962'>
		  <country>Isle of Man</country>
		  <record type='size'>Smallest Street-Legal Car at 99cm wide and 59 kg in weight</record>
		</car>
		<car name='Royale' make='Bugatti' year='1931'>
		  <country>France</country>
		  <record type='price'>Most Valuable Car at $15 million</record>
		</car>
	  </records>
	'''
  }
  
//使用DomToGroovy进行反向的转化:
import javax.xml.parsers.DocumentBuilderFactory
import org.codehaus.groovy.tools.xml.DomToGroovy

def builder     = DocumentBuilderFactory.newInstance().newDocumentBuilder()
def inputStream = new ByteArrayInputStream(XmlExamples.CAR_RECORDS.bytes)
def document    = builder.parse(inputStream)
def output      = new StringWriter()
def converter   = new DomToGroovy(new PrintWriter(output))

converter.print(document)
println output.toString()
打印出的结果如下:
//红色表示元素, 绿色表示属性, 黄色表示元素的text值
records() {
  car(make:'Holden', name:'HSV Maloo', year:'2006') {
    country('Australia')
    record(type:'speed', 'Production Pickup Truck with speed of 271kph')
  }
  car(make:'Peel', name:'P50', year:'1962') {
    country('Isle of Man')
    record(type:'size', 'Smallest Street-Legal Car at 99cm wide and 59 kg in weight')
  }
  car(make:'Bugatti', name:'Royale', year:'1931') {
    country('France')
    record(type:'price', 'Most Valuable Car at $15 million')
  }
}

三 为创建的XML添加NameSpace
实例代码: 
def xml = new MarkupBuilder(writer)
xml.'rec:records'('xmlns:rec': 'http://groovy.codehaus.org') {
  car(name:'HSV Maloo', make:'Holden', year:2006) {
    country('Australia')
    record(type:'speed', ' Truck with speed of 271kph')
  }
}

result

<rec:records xmlns:rec='http://groovy.codehaus.org'>
  <car name='HSV Maloo' make='Holden' year='2006'>
    <country>Australia</country>
    <record type='speed'> Truck with speed of 271kph</record>
  </car>
</rec:records>

=================================
xml.records(xmlns: 'http://groovy.codehaus.org') {
  car(name:'HSV Maloo', make:'Holden', year:2006) {
    country('Australia')
    record(type:'speed', ' Truck with speed of 271kph')
  }
}

result

<records xmlns='http://groovy.codehaus.org'>
  <car name='HSV Maloo' make='Holden' year='2006'>
    <country>Australia</country>
    <record type='speed'> Truck with speed of 271kph</record>
  </car>
</records>


四 使用Groovy的StreamingMarkupBuilder创建XML:
示例代码:

import groovy.xml.StreamingMarkupBuilder

 def xml = new StreamingMarkupBuilder().bind {
    root {
      a( a1:'one' ) {
        b { mkp.yield( '3 < 5' ) } //注意点
        c( a2:'two', 'blah' )
      }
    }
  }
 
 println xml.toString();
 程序运行结果:
  <root>
      <a a1='one'>
          <b>3 < 5</b>
          <c a2='two'>blah</c>
      </a>
  </root>
  
五 使用Groovyapi结合Dom创建XML
   参考链接:http://groovy.codehaus.org/Creating+XML+with+Groovy+and+DOM


 

Avatar_small
Ali Darley 说:
2018年7月15日 17:27

Learners of process designing will praise you a lot with study of this share that provides useful knowledge to them. Students of master classes get help from custom dissertation services online for writing of their works.

Avatar_small
Steve Johny 说:
2018年11月12日 12:31

When the <a href="https://essayschief.com/">Cheap Essay Writing Service</a> conducts a study on Computer application students for a poll purpose, most of them state that they are refer and check websites who providing the Program basics and samples

<!--td {border: 1px solid #ccc;}br {mso-data-placement:same-cell;}-->
Avatar_small
seo service london 说:
2024年1月14日 21:55

Hello, I have browsed most of your posts. This post is probably where I got the most useful information for my research. Thanks for posting, maybe we can see more on this. Are you aware of any other websites on this subjec


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter