my program is as follows:
//import the relevant packages.
import com.dbxml.db.client.CollectionClient;
import com.dbxml.db.client.ResultSetClient;
import com.dbxml.db.client.dbXMLClient;
import com.dbxml.db.client.xmlrpc.dbXMLClientImpl;
import com.dbxml.util.dbXMLException;
import java.util.HashMap;
import java.io.*;
import java.lang.Object;
public class Example2 {
public static void main(
String[] args) {
try {
// Connect to the database on localhost port 7280
dbXMLClient client = new dbXMLClientImpl();
client.setProperty(dbXMLClient.USER, "scott");
client.setProperty(dbXMLClient.PASS, "tiger");
client.connect();
// Retrieve a CollectionClient reference for /testCollection
CollectionClient col = client.getCollection("/testCollection");
// set TIME variable for how long you want the program to run
double TIME = 0.3;
// Build up the query strings and create an empty namespace map
StringBuffer sb = new StringBuffer();//string buffer to contain timestamp xpath query .
StringBuffer sbseq = new StringBuffer();//string buffer to contain sequence number xpath query.
if ((null !="/pdml/packet/proto/field[@name ='timestamp']")&&(null !="/pdml/packet/proto/field[@name ='tcp.seq']")) {
sb.append("/pdml/packet/proto/field[@name ='timestamp']");//append the time stamp query
sbseq.append("/pdml/packet/proto/field[@name ='tcp.seq']");//append the sequence number query
}
String query = sb.toString();//convert them to strings
String queryseq = sbseq.toString();
// System.out.println("queryseq is" + queryseq);
HashMap nsMap = new HashMap();//Set up a hashmap for timestamp
HashMap nsMapseq = new HashMap();//Set up a hashmap for sequence number
// Perform the query and iterate over its ResultSetClient.
ResultSetClient rs = col.queryCollection("XPath", query, nsMap);//for timestamp
ResultSetClient rsseq = col.queryCollection("XPath", queryseq, nsMapseq);//for sequence number info.
//set variables needed to set the time the initial packet goes through ie. starttime
int flag=0;
double starttime=0;
//while there are more results from timestamp results
while ( rs.next() )
{ //get the next timestamp value and sequence number value
rsseq.next();
{// convert each of these values in to text.
String result = rs.getResultAsText();
String resultseq = rsseq.getResultAsText();
System.out.println(" resultseq is " +resultseq);
//get out substring from timestamp to contain just the timestamp.
//first find where the value attribute starts in the located XPath string
int startpoint =result.indexOf("val");
System.out.println("here a");
int startpointseq =resultseq.indexOf("val");
System.out.println("here b");
// now find where the actual value we are interested in starts
String tsresult= result.substring((startpoint+7),(startpoint+27));
String tsresultseq= resultseq.substring((startpointseq+7),(startpointseq+15));
//and convert the timestamp string into a double
// for calculation in relation to starting time
double tsresultdouble = Double.parseDouble(tsresult);
//if starting time has not already been set
if (flag ==0)
//then set it.
starttime =tsresultdouble;
//print out the start time
System.out.println("start time is " +starttime);
// it is now set so change the flag so this can't be done again
flag=+1;
// if a packet time stamp is within the time period we want
if ((tsresultdouble-starttime)<=TIME)
{// then print out the timestamp and sequence number
System.out.println(tsresult);
System.out.println("sequence result tresultseq is " + tsresultseq);
}
else //if the packet does not fit in this time period
//then close the ResultSetClient and Disconnect from the database
{
rs.close();
client.disconnect();
}
}
}
}//error handling
catch ( dbXMLException e ) {
e.printStackTrace(System.err);
}
}
}
It connect with a database dbXML and queries it using xpath.
It works except that I want it to only print 'timestamp' and 'tcp.seq' when both these values are in a packet (i.e within <packet>
. At the moment it just print the first 'timestamp' and 'tcp.seq' values it comes to so that when the 'tcp.seq' is absent it results in the 'timestamp' and 'tcp.seq' values not correlating (i.e. not being from the same packet).
I tried putting in the code section
StringBuffer sbseq = new StringBuffer();//string buffer to contain sequence number xpath query.
if ((null !="/pdml/packet/proto/field[@name ='timestamp']")&&(null !="/pdml/packet/proto/field[@name ='tcp.seq']")) {
sb.append("/pdml/packet/proto/field[@name ='timestamp']");//append the time stamp query
sbseq.append("/pdml/packet/proto/field[@name ='tcp.seq']");//append the sequence number query
}
String query = sb.toString();//convert them to strings
but this doesnt seem to have any effect. Its probably obvious to others but what am I doing wrong.
Thanks in advance for any help.
Adam