groovyrcv.groovy

来自「开源的axis2框架的源码。用于开发WEBSERVER」· GROOVY 代码 · 共 58 行

GROOVY
58
字号
import java.io.*;
import javax.xml.parsers.DocumentBuilderFactory;
import org.apache.xpath.XPathAPI;
import groovy.xml.*
import java.io.StringWriter;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.*;
import org.xml.sax.*;

class GroovyRcv{

	public static void main(args){
        String value = "<ADDRESS><DET><NAME>Ponnampalam Thayaparan</NAME> <OCC>Student</OCC><ADD>3-2/1,Hudson Road,Colombo-03</ADD><GENDER>Male</GENDER></DET><DET><NAME>Eranka Samaraweera</NAME><OCC>Student</OCC><ADD>Martara</ADD><GENDER>Male</GENDER></DET><DET><NAME>Sriskantharaja Ahilan</NAME><OCC>Student</OCC><ADD>Trincomalee</ADD><GENDER>Male</GENDER></DET></ADDRESS>"
		println new GroovyRcv().echo(new StringReader(value))
    }

	Object echo(StringReader arg){

	 List addNodeValues = []
	 List nameNodeValues = []
	 int countEmp = -1

	 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance()
	 DocumentBuilder builder = factory.newDocumentBuilder()
	 Document doc = builder.parse(new InputSource(arg))

	 NodeList detNodes = XPathAPI.selectNodeList(doc,"ADDRESS/DET")
	 for(detNodeIndex in 0..(detNodes.getLength()-1)) {
		 NodeList nameNodes = XPathAPI.selectNodeList(detNodes.item(detNodeIndex),"NAME")
	
		 for(nameNodeIndex in 0..(nameNodes.getLength()-1)) {
			 Node nameNodeValue = nameNodes.item( nameNodeIndex ).getFirstChild()
			 nameNodeValues += [nameNodeValue]
		 }
	
		 NodeList addNodes = XPathAPI.selectNodeList(detNodes.item(detNodeIndex),"ADD")
		 for(addNodeIndex in 0..(addNodes.getLength()-1)) {
			 Node addNodeValue = addNodes.item(addNodeIndex).getFirstChild()
			 addNodeValues += [addNodeValue]
		 }
		 countEmp++
	 }//for

	StringWriter writer = new StringWriter()
    MarkupBuilder xmlBuilder = new MarkupBuilder(writer)
	xmlBuilder.person(){
	 	for(x in 0..countEmp){
			employee(){
			name(nameNodeValues[x])
			address(addNodeValues[x])
			tdate(new java.util.Date())
	 	}
	  }
	 }

   return writer
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?