Hi,
I have to extract an correlate many values from an XML feed. I then need to
sort the entries, store them, and compare them against subsequent feeds.
Given the complexity of the task, I decided to do it with a Beanshell
Post-Processor. I first spiked the code in Java and everything worked. However
when I moved it into JMeter/Beanshell, the evaluation of the XPath expressions
always returns 0 nodes. Making the Document namespace-aware makes no
difference. The document parses fine, and printing out "new String(data,
"UTF-8")" displays the correct data returned by the sampler.
Can anyone think of any reasons why this could be happening?
Here's the code:
>>>>>>>>>>>>>>>>>>>>
import org.w3c.dom.*;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import org.xml.sax.*;
log.info("Starting "+getSourceFileInfo());
log.debug("Parameters: " + Parameters);
startTime = System.currentTimeMillis();
params = Parameters.split("\\|");
sns = params[0];
// Method for retrieving Node text value
public static String getNodeValue(Node node)
{
if(!node.hasChildNodes()) return "";
else return node.getFirstChild().getNodeValue();
}
// Build XML document from the response
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(false);
DocumentBuilder builder = domFactory.newDocumentBuilder();
InputSource inStream = new InputSource();
inStream.setCharacterStream(new StringReader(new String(data, "UTF-8")));
Document doc = builder.parse(inStream);
XPath xpath = XPathFactory.newInstance().newXPath();
// Create XPath Expressions
XPathExpression updatedExpr = xpath.compile("/feed/entry/updated");
XPathExpression displayNameExpr = xpath.compile("/feed/entry/author/name");
XPathExpression statusExpr = xpath.compile("/feed/entry/summary");
XPathExpression snsExpr = xpath.compile("/feed/entry/source/author/name");
// Evaluate XPath Expressions
NodeList updatedNodes = (NodeList)updatedExpr.evaluate(doc,
XPathConstants.NODESET);
NodeList displayNameNodes = (NodeList)displayNameExpr.evaluate(doc,
XPathConstants.NODESET);
NodeList statusNodes = (NodeList)statusExpr.evaluate(doc,
XPathConstants.NODESET);
NodeList snsNodes = (NodeList)snsExpr.evaluate(doc, XPathConstants.NODESET);
System.out.println("updatedNodes: "+updatedNodes.getLength());
System.out.println("displayNameNodes: "+displayNameNodes.getLength());
System.out.println("statusNodes: "+statusNodes.getLength());
System.out.println("snsNodes: "+snsNodes.getLength());
// Create data types
/* HashSet<List> set = new HashSet<List>();
TreeMap<Date, HashSet<List>> tree = new TreeMap<Date, HashSet<List>>();*/
// Put values in data structure
for(int i =0; i < updatedNodes.getLength(); i++)
{
String u = getNodeValue(updatedNodes.item(i));
String d = getNodeValue(displayNameNodes.item(i));
String s1 = getNodeValue(statusNodes.item(i));
String s2 = getNodeValue(snsNodes.item(i));
// do more stuff
}
// Store tree in Object variable
//vars.putObject("individual.friends.statuses.tree", tree);
finishTime = System.currentTimeMillis();
log.info("Finished "+getSourceFileInfo()+" (" + (finishTime-startTime) +
"ms)");
<<<<<<<<<<<<<<<<<<<<
JMeter: 2.3.2
Java 1.6.0_12
OS: Linux
Regards,
Noel