File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes XML and Related Technologies and the fly likes xpath problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "xpath problem" Watch "xpath problem" New topic
Author

xpath problem

adam reid
Greenhorn

Joined: Oct 04, 2004
Posts: 3
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
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
if ((null !="/pdml/packet/proto/field[@name ='timestamp']")&&(null !="/pdml/packet/proto/field[@name ='tcp.seq']")) {

What you're doing there is comparing null and a constant string (which just happens to represent an XPath expression). That'll never evaluate to true...


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
And you're apparently doing the same in many other places in the code you posted. String literals are string literals in Java, no matter what their value is. If you want to evaluate XPath expressions against an XML document in Java, you need to use an API like Jaxen or the .XPath class from Apache Xerces.
adam reid
Greenhorn

Joined: Oct 04, 2004
Posts: 3
Ok thanks for the advice. Ive thought about the problem again.
returning to the original program:



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
[ October 06, 2004: Message edited by: Madhav Lakkapragada ]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Hi again Adam,

As a small suggestion, you might get a better response if you'd encapsulate your Java code snippets with the UBB [CODE] tags to make them more readable (preserving indentation).
adam reid
Greenhorn

Joined: Oct 04, 2004
Posts: 3
Ok cheers.



Is that better? Any suggestions?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
That is better but now you don't have any indentation left. If you had, it would be displayed just perfectly.
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040

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.



Well in that case, you should first make sure that the two sets are
of the same size before you get into the while loop.

When you enter the while loop the way you are, there seems to be
an implecit assumption that the lists are equal.

When the lists are not equal, you need to add more busines logic
choosing some other criteria (I can't suggest what it is based on
your post) and address the fact that some of the tcp.seq values
are not present.

Hopefully I am not going in a off-tangent here....

Thanks.


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: xpath problem