Java program using xpath on dbXML database

Questions about XML that are not covered by the other forums should go here.
xpathman
Posts: 4

Java program using xpath on dbXML database

Wed Oct 06, 2004 4:59 pm

I have a bit of a problem. 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.




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);
}
}
}

This works in as far as it reads in all 'timestamp' and 'tcp.seq' values.

But if there are packets with 'timestamp' values and no 'tcp.seq' values then when the results are printed out the time stamp values do not correspond to the releveant sequence numbers for any packet ie. the values are all out of sync.

The problem starts at

// 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.


It would be better if I could create a value into 'rs' only if the accompanying packet 'tcp.seq' query existed.

Ive based my program on

http://www.dbxmlgroup.com/docs/programmer.html

I accept there are some things Im still struggling to grasp


I want to be able to rectify this. I would appreciate if any one had some suggestions. thanks again.

Adam
--------------------------------------------------------------------------------
thanks for any help
george
Site Admin
Posts: 2100

Thu Oct 07, 2004 4:44 pm

Hi Adam,

I did not get through all your post but this might help.

Instead of selecting the fields based only on a single check you should verify also that when you have a timestamp field you also have a tcp.seq field, like below:

/pdml/packet/proto/field[@name='timestamp' and ../field/@name='tcp.seq']

Best Regards,
George

Return to “General XML Questions”

Who is online

Users browsing this forum: No registered users and 0 guests