2

I am using Jmeter for Service Call.I am getting response in xml format.With xml response, I am also getting title above it which is not part of the xml response.Below is the response i am getting

TOKEN

<?xml version="1.0" encoding="UTF-8"?>
<xml>
    <URL>https://link.com</URL>
    <ToName>sharnell Kkqwjidzwh</ToName>
    <Link>gsajfgasgfgasjkgfjasgfjgjg</Link>
</xml>

I want to extract the value of Link attribute.I tried this path in xpath extractor..

//Link But it isnt working.I think it is because of TOKEN title at top of response.Do anyone one know how we can edit the response and remove that title from response.

Thanks in Advance

2 Answers2

3

First of all try checking Use Tidy box, it may help in case of non-valid XML/XTML

If it doesn't help - see below workaround:

If you need to strip off everything before <?xml version="1.0" encoding="UTF-8"?> line you can do it via Beanshell PostProcessor.

  1. Add a Beanshell PostProcessor as a child of the request which returns your TOKEN and XML
  2. Make sure that Beanshell PostProcessor is before the XPath Extractor
  3. Put the following code into Beanshell PostProcessor's "Script" area:

    String originalResponse = new String(data);
    String filteredResponse = originalResponse.substring(originalResponse.indexOf("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"));
    prev.setResponseData(filteredResponse.getBytes());
    

The above code will override the response and cut everything which is before <?xml version="1.0" encoding="UTF-8"?> line

Explanations:

  • data - is byte array containing parent sampler's response data
  • substring and indexOf - are basic java.lang.String methods
  • prev is an instance of SampleResult class which provides read/write access to parent sampler's result

See How to use BeanShell: JMeter's favorite built-in component guide for more information on Beanshell scripting in JMeter.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thanks!!Its working.Do you have any idea on how to edit the value of "Link" attribute i have fetched as i don't want full of it. – Shashank Lahariya Aug 12 '15 at 12:30
  • [XPath Substring](https://developer.mozilla.org/en-US/docs/Web/XPath/Functions/substring) function may be what you're looking for – Dmitri T Aug 13 '15 at 08:42
-1

If you want to extract just Link value, you can use regular expression extractor. Here you can find some docs:

http://jmeter.apache.org/usermanual/regular_expressions.html

https://docs.blazemeter.com/customer/portal/articles/1743642

http://www.tutorialspoint.com/jmeter/jmeter_regular_expressions.htm

automatictester
  • 2,436
  • 1
  • 19
  • 34
  • [It is a poor idea to use regular expressions to parse xml and html](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags). xpath extractor exists for a reason. – RaGe Aug 12 '15 at 15:25
  • Well, I can't disagree with you. XML should be parsed with XPath. But above is not an example of valid XML document as it has invalid TOKEN element in the first line. Beanshell slows things down, that's why I'm not willing to support using it, unless necessary. – automatictester Aug 12 '15 at 15:34