1,075,642 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Posts by srinivas88

Hi,
i m trying to ssh to a server and run a exe on that using a script file - runremote.sh

All the prints generated by the exe on remote are being displayed on server 1 from where i have executed the script . But after sometime, the execution stops suddenly, dont know why .. is it that the ssh tunnel is getting broken /some ssh timeout happening??

the content in script file is something like

ssh root@server2ip 'cd /export/home1/users/srinivas;
sh test.sh start 1;'

and test.sh is executing a exe (say test.out which is printing somestuff in while loop)

Initially everything works fine , but then after some time (1min), the execution stops .. cant understand y .. any ideas??

srinivas88
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
srinivas88
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

thanks for your help..

one more query ..

result=`$connect $db -e "select CheckSum from DbCheckSumTbl where DbName=\"$FILE\""`

works, but ..

sqlquery="select CheckSum from DbCheckSumTbl where DbName=\"$FILE\""
result=`$connect $db -e $sqlquery`

doesnt work .. any bright ideas??

srinivas88
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I have modified the sql syntax as ...

    result=`mysql -u $sqluser $db -e "select CheckSum from DbCheckSumTbl where DbName=\"$FILE\""`

    CHECKSUM_ORIG=`echo $result |cut -f2 -d ' '`

and the value is getting stored properly. Do let me know if there is a better way.
The return status can be checked using ...

if [ $? -eq 0 ]; then
echo "Success"
else
echo "Failure"
fi

if something goes wrong with connect, then "$checksum_orig" != "$checksum"
there, if $checksum_orig is not a number, then it's probably an error message from mysql; isn't it?

$checksum_orig and $checksum both variables are strings. so when the query fails, $checksum_orig will contain ntg and the prog will report the file as corrupted.

Also there is a error in the line . whose trace is given under that.
checksum_orig=$(printf '%s\n' "use $db" "$sqlquery" | connect)

  • sqluser=root
  • sqlpasswd=
  • db=CHECKSUM_DB
  • file=test.db
    ++ cksum test.db
  • checksum='134664090 92 test.db'
  • checksum='134664090 92 test.db'
  • sqlquery='select CheckSum from DbCheckSumTbl where DbName="test.db"'
  • test -z ''
    ++ printf '%s\n' 'use SGRAN_CHECKSUM_DB' 'select CheckSum from DbCheckSumTbl where DbName="test.db"'
    ++ connect
    ++ connect -p ''
    ++ connect -p ''
    ++ connect -p ''
    ..

tq.

srinivas88
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

How do you verify for negative cases like the return values for connect (if password is given wrong) and if the sql query fails?? Means how do u check for return values??
That $sqlquery will return the column name (CheckSum) and its value, I used cut so that only the value is returned.
On running the script using bash -x i get the following trace...

checksum_orig='CheckSum
1616473790'

i.e., checksum_orig is containing both column name and field. So u might need to do some more modifications..

Also do let me know of any useful ebooks/links on this topic..

srinivas88
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi,
I am trying to use mysql select query in a shell script but i need to know how to store a particular value from the output of the query in a script variable.
my script file is given below.
I need to store the value under field CheckSum in the script variable $CHECKSUM_ORIG .There is only 1 entry in the db. The line
CHECKSUM_ORIG=$sqlquery;
doesnt work and gives a error. Please let me know the correct syntax/how to do it. If you have any link to a book on that u can share it as this is the 1st time i m using sql from scripts..
The program below is supposed to calculate the current check sum of a file and compare it with the checksum value already stored in mysql. If the value is same that means that the file is not corrupted.

        #!/bin/sh
        # Run using ./computeChecks.sh <filename>
        # Shell script to compare cksum difference between two files 

        sqluser="root"
        sqlpasswd=""
        db="CHECKSUM_DB"

        FILE=$1
        CHECKSUM_ORIG=""

        start () {

            CHECKSUM=`cksum "$FILE" | cut -f1 -d" "`

            sqlquery="select CheckSum from DbCheckSumTbl where DbName='$FILE'"
            #echo -e "$sqlquery"

            if [ -n "$sqlpasswd" ]; then
            connect=" mysql -u "$sqluser" -p "$sqlpasswd" "
            else
            connect=" mysql -u "$sqluser" "
            fi
            eval $connect <<EOF
            use $db;
            CHECKSUM_ORIG=$sqlquery; 
        EOF
        #**** the line b4 EOF doesnt work *****#
        #CHECKSUM_ORIG=$sqlquery;
        #CHECKSUM_ORIG=`$CHECKSUM_ORIG| cut -f1 -d ""`
        #echo $CHECKSUM_ORIG
        #echo $CHECKSUM

            if [ "$CHECKSUM_ORIG" != "$CHECKSUM" ]; then
                    echo "corrupted file: $FILE"
                    exit 0
            else
                    echo "file ok: $FILE"
                    exit 1
            fi

        }
        start
srinivas88
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

ok, atleast i am able to kill test.sh, only thing is since i had that file opened in gvim, that is also getting killed. Since i will be running test.sh in a dedicated system, some other processes with name test.sh shldn't come up and get killed with pkill.

Now for just testing purpose is der any other option with `pkill` which will not `kill gvim` which has `test.sh` opened .. ??

Anyway that -f option helps..tq

srinivas88
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi,

I am writing a script which will run a process in a while loop, i.e., user will execute
sh test.sh

and the script will run a set of processes,
I want to know how to kill the script which is having a infinite while loop in its contents.

Killing a process (say test.out) is done by
pkill -9 test.out

and a script can be killed using
pkill -9 test.sh

But this is possible only when the user has started the script using
./test.sh

But if the user runs like this
sh test.sh

Then it cannot be killed using
pkill -9 test.sh

Since both options to run the script should be given to the user, in what way can i kill the script in case the user starts the script as
sh test.sh

I am using fedora 16.
Below is a sample code of what i am trying to do. You can create a test.out executable with following content

#include <stdio.h>
#include <iostream>

int main ()
{
    int i=1;
    while (i)
    {
        printf ("%d  ",i);
        i++;
        sleep (1);
    }

return (0);
}

the contents of script file test.sh is as follows

#!/bin/sh
# Run using ./test.sh  start
# Stop using sh test.sh stop   or  ./test.sh stop

start () {
while [ 1 ]
do
   echo "Starting.."
   ./test.out
   #./test2.out  
   sleep 3
done
}
stop () {
   echo "Stopping run.sh script"
    pkill -9 run.sh
    #** The above line works only if user has run with ./test.sh start
    # but not if the user has used sh test.sh start **
   exit 
}
case "$1" in
   start)
           start
       fi
       ;;
   stop)
       stop
       ;;
   *)
       echo "Invalid argument"

esac

srinivas88
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi,
i'm using the following cmd to remove all lib files ending with .a

find . -name "*.a" -exec rm -rf {} \;

I have a file which i dont want to delete , say test.a,
I just want to know how to make rm not delete test.a,
but delete rest all files ending with .a

srinivas88
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Hi,
I'm running a script to bring down the logical ip address . But if the system doesnt have that logical ip addresss then i get the following error print on terminal -

SIOCSIFFLAGS: Cannot assign requested address

I just want to know whether there is a way to suppress that print so that user is not aware of that.
May be some check before pulling down the logical ip.

the script file content is -

ifconfig eth0:1 down

is there any syntax by which i can check whether eth0:1 is up or not .
My purpose is not to show the user that error msg and i wanna do it from the script file itself not modify some system files as i wouldnt know on which (linux) system the user will be excuting that script.

srinivas88
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

*What is likely happening is that you have the first message queued by the OS before there is a notification that the remote end was down and only on the second send do you see the error.

when i send msgs before the pipe is broked, the OS sends them immediately , then y is it queuing the 1st msg when the socket is broken??
See if u can find a way around this..
the client and server code is ..

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <memory.h>
#include <errno.h>
#include <signal.h>

using namespace std;


int MAXLEN=1024;
int PORT=20000;

void sigHlr(int sigNo)
{
	cout << "SIGNAL Received " << sigNo;
}

main( int argc, char *argv[])
{
	struct sockaddr_in serv;
	int sockfd,newsockfd;
	char mesg[MAXLEN];
	char reply[MAXLEN];
	int port = PORT;

	signal(SIGPIPE, sigHlr);	
	if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
	{
		cout << "client : socket error.\n";
		exit(1);
	}
	bzero((char *)&serv,sizeof(serv));
	
	serv.sin_family = AF_INET;
	serv.sin_addr.s_addr = inet_addr("127.0.0.1");
	serv.sin_port = htons(port);
	
	if( argc == 2 )
	{
		serv.sin_addr.s_addr = inet_addr("127.0.0.1");
		port = atoi(argv[1]);
		serv.sin_port = htons(port);
		
	}
	
	else if ( argc == 3 )
	{
		serv.sin_addr.s_addr = inet_addr(argv[2]);
		port = atoi(argv[1]);
		serv.sin_port = htons(port);
	}
	
	if ( connect(sockfd,(struct sockaddr *)&serv,sizeof(serv)) < 0)
	{
		cout << "client : connect error.\n";
		exit(1);
	}

	cout << "Client connected to server successfully at port:" << port << endl;

	while(1)
	{
		cout << endl << "Enter message to server : (Enter q to quit)" << endl;
		cin.getline(mesg,MAXLEN,'\n');
	
		if( strcmp(mesg,"q") == 0 )
		{
			close(sockfd);
			break;
		}
		cout << "Message sending is: " << mesg <<endl;
		if ( send(sockfd,mesg,strlen(mesg)+1,0) < 0  || errno == -1 )
		{
			perror("send");
			//close(sockfd);
			//exit(1);
			
			// for implementing redundancy at MMI client
			sleep(10);
				if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
				{
					cout << "client : socket error.\n";
					exit(1);
				}
				bzero((char *)&serv,sizeof(serv));
				
				serv.sin_family = AF_INET;
				serv.sin_addr.s_addr = inet_addr("127.0.0.1");
				serv.sin_port = htons(port);
				
				if( argc == 2 )
				{
					serv.sin_addr.s_addr = inet_addr("127.0.0.1");
					port = atoi(argv[1]);
					serv.sin_port = htons(port);
					
				}
				
				else if ( argc == 3 )
				{
					serv.sin_addr.s_addr = inet_addr(argv[2]);
					port = atoi(argv[1]);
					serv.sin_port = htons(port);
				}
				
				if ( connect(sockfd,(struct sockaddr *)&serv,sizeof(serv)) < 0)
				{
					cout << "client : connect error.\n";
					exit(1);
				}
		}
/*
		if ( recv(sockfd,&reply,MAXLEN,0) < 0 )
		{
			perror("recv");
			close(sockfd);
			exit(1);
		}
		cout << endl << "client: Received message " << endl;
		cout << "Message is : " << reply <<endl;
*/
	}
	close(sockfd);
}


//============================================================================
// End of tcpClient.cpp
//============================================================================

server:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/errno.h>

using namespace std;

int STRLEN = 1024;	
int PORT = 20000;


int main( int argc, char *argv[])
{
	int sockfd;	
	int newsockfd;
	int clilen;
	struct sockaddr_in serv,cli;
	int chpid;
	char buf[STRLEN],ack[STRLEN];
	int port = PORT;

	if ( argc == 2 )
	{
		port = atoi(argv[1]);
	}

	if ((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0)
	{
		perror("socket");
		exit(0);
	}

	bzero((char *)&serv,sizeof(serv)) ;

	serv.sin_family = AF_INET;
	serv.sin_addr.s_addr = htonl(INADDR_ANY);
	serv.sin_port = htons(port);

	if (bind(sockfd,(struct sockaddr *)&serv,sizeof(serv)) < 0)
	{
		perror("bind");
		exit(1);
	}

	listen(sockfd,5);

	while (1)
	{
		clilen = sizeof(cli);
		cout << "Server waiting for connection request from client " << endl;
		if ( (newsockfd = accept(sockfd,(struct sockaddr *)&cli,(socklen_t *)&clilen)) < 0 )
		{
			perror("accept");
			close(sockfd);
			exit(1);
		}
		cout << "server : accepted connection from client  at port: " << cli.sin_port <<endl;
		
		if( (chpid = fork()) == 0 )
		{
			//Child Process
			//Handle request and exit
			cout << "I am child process on server .. I reply for your request " << endl;
			close(sockfd);
			while ( recv(newsockfd,&buf, STRLEN, 0) > 0 )
			{
				cout << endl << "server : received message " <<endl;
				cout << "Message is :" << buf << endl;	
				sprintf(ack,"%s ACK",buf);
				cout << endl << "server sending message to client" << endl;
				cout << "Message is :" << ack << endl;
				if ( send(newsockfd,ack,strlen(ack)+1,0) < 0 )
				{
					perror("send");
					close(newsockfd);
					exit(1);
				}
			}
			close(newsockfd);
			cout << "Child is exiting" << endl;
			exit(1);
		}
	
		else if(chpid < 0 )
		{
			cout << "Fork error\n";
			exit(1);
		}
		else
		{
			//Parent process
			close(newsockfd);
		}
	}

}

//============================================================================
// End of tcpServer.cpp
//============================================================================
srinivas88
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

So how do I make a change such that I receive the signal immediately after I send 1st message??

srinivas88
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I am basically handling this signal and making the client reconnect to the server (server rebootes after it goes down) . As long as I dont receive a sigpipe signal i wouldnt know that the pipe is broken.

Also Is there any other way in which the client can detect that the server has gone down. It must detect immediately that the connection is broken and try to reconnect with the server.

srinivas88
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

In a client server program ... generally when the server goes down the client on executing the send command 2 times returns a SIGPIPE signal . I want to know y this happens. The client must recieve sigpipe immediately when it sends a message after the socket connection is broken . I have tried many versions of client server programs , when i close the server down , client after it sends 2 messages only is it able to receive a sigpipe signal, shouldn't it be like the cleint receives sigpipe immediately after it sends any msg ??

srinivas88
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Actually here a warm stand by is used ... that is at any point there will be only 1 process receiving the messages , later after it enters another state it sends a message to the standby to update its state ... this way there is no un necessary link flooding due to the standby process ...
May be one way of doing this can be that process 3 will be given a logical ip to which it will keep on sending messages and it wouldnt be aware that a switch over has happened .. how can we give a single logical ip to the group of processes 1 and 2 .. so that process 3 keeps on sending messages without being aware of a switch over happening (if any)..

srinivas88
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I am trying to implement process pairs redundancy (process level redundancy) in c/c++.
basically there is a active core process 1 running on one system and its standby copy, process 2, is running on other system.

When ever the active one goes down the standby becomes active and executes the rest of the processing.Mean while the process 1 which went down rebootes and it now behaves as the standby process.

Now there is another process 3 on another system which is sending some (TCP) messages to the active process (may be process 1 or 2 depending which is currently the active one) . One approach to process redundancy is that process 3 will be working as if there is only one process receiving/ sending back messages to it.

What i want to know is how that can be achieved. Process 3 will be given the ip address and port no. for the active process. Since the active process can be either process 1 or 2 (which are on diff. systems with different ip address/port no's) .

Is it possible to switch the ip address of process 1 or 2 depending on it is active or not?? The actual problem is that of bigger systems involving lot of processing which will involve a remote process (3) sending it messages on what set of processing to do next. I just want to know how it is done at the basic level so that i can apply it to the bigger level. May be a simple message sending and receiving programs between processes 1,2 to 3 .

Any help on the approach, possible solutions by which we can implement process pairs redundancy or even some sample progs will be greatly appreciated. The main issue here is of connectivity rather than how the messages are sent/received.

srinivas88
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

can you tell in what sense 10.10 is unstable? and when can we expect a stable version of it.I'm using ubuntu more frequently now, mostly for coding purpose..

srinivas88
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Well tried all that..Anyways my problem is solved now..it seems i had given the installation size of ubuntu as 7GB at the time of installing it along with windows vista..Overtime that got filled..Another problem was that i had not cleaned my tmp folder which had a lot of crap..I cleaned it by navigating to /tmp and typing the command [TEX]rm *[/TEX]
with root previleges(sudo su)..Now I have installed ubuntu 10.10 with an installation size of 15GB ,I think that should be enough..I also got all the previous softwares that were installed in 9.04 to 10.10 with the help of selections, will give more info on how i did that if anyone wants...:icon_smile:

srinivas88
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I am trying to upgrade ubuntu from 9.04 to 9.10.. but the installation says that i have not enough space on /. How do i add the additional space.any command for that? I have installed ubuntu inside windows and the drive in which ubuntu is installed has a little more than 5GB of free space..my swap memory is around 2.6GB (on a 2GB RAM,core2Duo processor) and the total space on that drive is around 20GB.
Also let me know whether plain upgradation to 9.10 will be usefull or not , do u suggest me to uninstall my previous version first and then install 9.10 or simple upgradation would do good? Also i wanna make sure that all the installed packages in my current system remain in 9.10 too..

Further more when i try to start synaptics package manager, i get this error..
Failed to run /usr/sbin/synaptic as user root.
unable to copy the user's Xauthorisation file

The output of df -h on my system is ..maybe this can help u in resolving my issue :p

Filesystem            Size  Used Avail Use% Mounted on
/host/ubuntu/disks/root.disk
                      7.5G  7.2G     0 100% /
tmpfs                 988M     0  988M   0% /lib/init/rw
varrun                988M  140K  988M   1% /var/run
varlock               988M     0  988M   0% /var/lock
udev                  988M  184K  988M   1% /dev
tmpfs                 988M  100K  988M   1% /dev/shm
/dev/sda6              20G   14G  5.9G  71% /host
lrm                   988M  2.2M  986M   1% /lib/modules/2.6.28-19-generic/volatile
overflow              1.0M  1.0M     0 100% /tmp
srinivas88
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

Help with DOM.I am trying to access the nodes acc. to their levels in an xml file.reached upto 3-4 levels then getting a nullpointer exception.Please help...wat datastructure would better suit this.i need to index all the nodes.the xml file is cs.xml given under and the java code below tht.

<department>
<deptname>Computer Sciences</deptname>
    <gradstudent>
    <name>
      <lastname>Aboulnaga</lastname>
      <firstname>Ashraf</firstname>
    </name>
    <phone>2626623</phone>
    <email>ashraf@cs.wisc.edu</email>
    <address>
      <city>Madison</city>
      <state>WI</state>
      <zip>53705</zip>       
    </address>
    <office>7360</office>
    <url>www.cs.wisc.edu/~ashraf</url>
    <gpa>4.0</gpa>
  </gradstudent>

  <gradstudent>
    <name>
      <lastname>Ailamaki</lastname>
      <firstname>Anastasia</firstname>
    </name>
    <phone>2652311</phone>
    <email>natassa@cs.wisc.edu</email>
    <address>
      <city>Madison</city>
      <state>WI</state>
      <zip>53706</zip>       
    </address>
    <address>
      <city>Madison</city>
      <state>WI</state>
      <zip>53706</zip>       
    </address>

    <office>7351</office>
    <url>www.cs.wisc.edu/~natassa</url>
    <gpa>4.0</gpa>
  </gradstudent>
  </department>


import org.w3c.dom.*;
import org.w3c.dom.Element.*;
import javax.xml.parsers.*;
import java.io.*;
import org.xml.sax.*;
import javax.xml.transform.*; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult;
public class project1{
  public static void main(String[] args) {
    try{
      BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Enter xml file name: ");
      String str = bf.readLine();
      String child[][]=new String[100][100];
      String parent[]=new String[100];
      String node[]=new String[100];
      String arr[][]=new String[100][100];
      NodeList fstNm1[][]=new NodeList[100][100];
      File file = new File(str);
      if (file.exists()){
        DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = fact.newDocumentBuilder();
        Document doc = builder.parse(str);
        NodeList list = doc.getElementsByTagName("*");
          int num=0,p1=num,p2=num;
          //L-0
          Element element = (Element)list.item(0);//root node
          node[num]=element.getNodeName();
          Node n=element.getParentNode();//root's parent node #doc
          parent[num]=n.getNodeName();
          NodeList fstNm = element.getChildNodes();//childs of root
          System.out.println("Node no: "+(num)+ " has name " +element.getNodeName()+" parent name "+n.getNodeName()+" and no.of childs is "+(fstNm.getLength()-1)/2);
          for(int j=1,k=0;j<fstNm.getLength();j+=2,k++)
          {
          System.out.println("Child: " + j/2 + " is " + fstNm.item(j).getNodeName()+" Parent: "+fstNm.item(j).getParentNode().getNodeName()  );
          child[num][k]=fstNm.item(j).getNodeName();
          }
          num++;
          p1=num;
          System.out.println((fstNm.getLength()-1)/2);
          //L-1
          for(int i=1,y=0;i<fstNm.getLength();i+=2,y++)
          {
          Element element1 = (Element)fstNm.item(i);
          node[num]=element1.getNodeName();
          Node n1=element1.getParentNode();
          parent[num]=n1.getNodeName();
          fstNm1[1][y] = element1.getChildNodes();
          System.out.println("Node no: "+num+ " has name " + element1.getNodeName()+" parent name "+n1.getNodeName()+" and no.of childs is "+(fstNm1[1][y].getLength()-1)/2);
          if((fstNm1[1][y].getLength()-1)/2==0)
          {
          child[num][0]="No Childs";
          }
          else
          {
          for(int j=1,k=0;j<fstNm1[1][y].getLength();j+=2,k++)
          {
          System.out.println("Child: " + j/2 + " is " + fstNm1[1][y].item(j).getNodeName()+" Parent: "+fstNm1[1][y].item(j).getParentNode().getNodeName()  );
          child[num][k]=fstNm1[1][y].item(j).getNodeName();
          }
          }
          num++;
          }
          p2=num;
        //L-2
          for(int y=0;p1<p2;p1++,y++)
          {
          for(int i=1;i<fstNm1[1][y].getLength();i+=2)
          {
          Element element2 = (Element)fstNm1[1][y].item(i);
          node[num]=element2.getNodeName();
          Node n2=element2.getParentNode();
          parent[num]=n2.getNodeName();
          fstNm1[2][y] = element2.getChildNodes();
          System.out.println("Node no:"+num+ " has name " + element2.getNodeName()+" parent name: "+n2.getNodeName()+" and no.of childs is "+(fstNm1[2][y].getLength()-1)/2);
          if((fstNm1[2][y].getLength()-1)/2==0)
          {
          child[num][0]="No Childs";
          }
          else
          {
          for(int j=1,k=0;j<fstNm1[2][y].getLength();j+=2,k++)
          {
          System.out.println("Child: " + j/2 + " is " + fstNm1[2][y].item(j).getNodeName()+" Parent: "+fstNm1[2][y].item(j).getParentNode().getNodeName()  );
          child[num][k]=fstNm1[2][y].item(j).getNodeName();
          }
          }
          num++;
          }
          }
          p2=num;
        //L-3
          for(int y=0;p1<p2;p1++,y++)
          {
          for(int i=1;i<fstNm1[2][y].getLength();i+=2)
          {
          Element element2 = (Element)fstNm1[2][y].item(i);
          node[num]=element2.getNodeName();
          Node n2=element2.getParentNode();
          parent[num]=n2.getNodeName();
          fstNm1[3][y] = element2.getChildNodes();
          System.out.println("Node no: "+num+ " has name: " + element2.getNodeName()+" parent name: "+n2.getNodeName()+" and no.of childs is "+(fstNm1[3][y].getLength()-1)/2);
          if((fstNm1[3][y].getLength()-1)/2==0)
          {
          child[num][0]="No Childs";
          }
          else
          {
          for(int j=1,k=0;j<fstNm1[3][y].getLength();j+=2,k++)
          {
          System.out.println("Child: " + j/2 + " is " + fstNm1[3][y].item(j).getNodeName()+" Parent: "+fstNm1[3][y].item(j).getParentNode().getNodeName()  );
          child[num][k]=fstNm1[3][y].item(j).getNodeName();
          }
          }
          num++;
          }
          }
          for(int i=0;i<num;i++)
          {
          System.out.println("Node name is "+node[i]+" Parent Name is "+parent[i]+" Child names are ");
          for(int j=0;j<100;j++)
          {
          if((child[i][j]!=null))
          System.out.println(child[i][j]+(i)+" "+j);
          }
          }
      }
      else{
        System.out.println("File not found!");
      }
    }
    catch(Exception e){
            System.out.println("Exception "+e);}
  }
}
srinivas88
Light Poster
39 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
 
© 2013 DaniWeb® LLC
Page rendered in 0.1204 seconds using 2.71MB