alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

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>Ponnampalam Thayaparan Student3-2/1,Hudson Road,Colombo-03MaleEranka SamaraweeraStudentMartaraMaleSriskantharaja AhilanStudentTrincomaleeMale"
		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
  }
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.