Apu
Apu

Reputation: 177

Extract elements from a xml response in jmeter according to a specific string in the element

enter image description hereenter image description hereenter image description hereI want to extract the contents of SFDCProductList in a variable, which is an array. How to do this?

I have this following xml: . . . 10000 Thank You! Your request has been successfully executed. Code PIM 10000 sgfsuifg sjkfbksgfsudf

      </SFDCProduct>
   </SFDCProductList>
   <SFDCProductList>
   .
   .
   .
</SFDCProductList>
<SFDCProductList>
   .
   .
   .
</SFDCProductList>
   <TransportInformation>
   .
   .
   .
   </TransportInformation>
</EnhancedServicePrequalResponse>

<EnhancedServicePrequalResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Code>10000</Code>
<Message>Thank You!</Message>
<SFDCProductList>
<SFDCProduct>
<OfferName>XYZ</OfferName>
<OfferDisplayName>New Offer</OfferDisplayName>
<OfferType>New</OfferType>
<EndDate>2021-02-16</EndDate>
<OfferLineItemList>
<OfferLineItem>
<OfferLineItemCategory>Transport Service</OfferLineItemCategory>
<OfferLineItemName>transport</OfferLineItemName>
</OfferLineItem>
<OfferLineItem>
<OfferLineItemCategory>Device</OfferLineItemCategory>
<OfferLineItemName>Billing</OfferLineItemName>
</OfferLineItem>
</OfferLineItemList>
<TransportName>BR</TransportName>
</SFDCProduct>
</SFDCProductList>
<SFDCProductList>
<SFDCProduct>
<OfferName>Upgrade</OfferName>
<OfferDisplayName>Upgrade</OfferDisplayName>
<OfferType>Upgrade</OfferType>
<EndDate>2021-02-16</EndDate>
<OfferLineItemList>
<OfferLineItem>
<OfferLineItemCategory>Transport</OfferLineItemCategory>
<OfferLineItemName>Billing</OfferLineItemName>
</OfferLineItem>
<OfferLineItem>
<OfferLineItemCategory>Device</OfferLineItemCategory>
<OfferLineItemName>Billing</OfferLineItemName>
</OfferLineItem>
</OfferLineItemList>
<TransportName>JR</TransportName>
</SFDCProduct>
</SFDCProductList>
<TransportInformation>
<TransportFeasibilityParameter>
<AvailabilityFlag>true</AvailabilityFlag>
<BusinessAvailabilityFlag>true</BusinessAvailabilityFlag>
<TransportName>BR</TransportName>
</TransportFeasibilityParameter>
<TransportFeasibilityParameter>
<AvailabilityFlag>true</AvailabilityFlag>
<BusinessAvailabilityFlag>true</BusinessAvailabilityFlag>
<TransportName>JR/TransportName>
</TransportFeasibilityParameter>
</TransportInformation>
</EnhancedServicePrequalResponse>

I tried with xpath extractor but its not storing it in a variable.

I have successfully extracted the SFDCProductlist with Boundary extractor but as its an array i only want that SFDCProductlist which has "New" keyword in the element .

The beanshell Script:

int SFDCProduct_matchNr =vars.get("SFDCProduct_matchNr");

String list="SFDCProduct"+"_"+"SFDCProduct_matchNr";
int SFDCProduct_matchNrvalue=SFDCProduct_matchNr-1;
vars.put("SFDCProduct_matchNr", "SFDCProduct_matchNrvalue");
vars.put("sfdc", "list");enter code here

But still its not assigning the sfdc with SFDCProduct_1 value??

Upvotes: 3

Views: 8342

Answers (3)

sunny_teo
sunny_teo

Reputation: 1999

From you response code, seems like there are more than one product list.

To fetch all, you need to set Match No. to -1 in the Boundary extractor. Then, use the vars.get{"Product_List_1"} to fetch specific one.

enter image description here

If you need all in one variable, one way, is to combine different array list.

With RegEx:-(.\d\n) enter image description here

Upvotes: 1

Dmitri T
Dmitri T

Reputation: 168227

Without seeing the full XML response we cannot come up with an exact solution, however for XML response types it makes sense to stick to XPath Extractor

The relevant XPath query should be something like: //SFDCProductList - it will basically return everything under <SFDCProductList> tag:

JMeter XPath Extractor Demo

If it doesn't - double check your XPath expression using "XPath Tester" mode of the View Results Tree listener, in some cases you might need to:

  1. Tick Use Tidy box if the response is not valid XML/XHTML
  2. If there are XML Namespaces in the response you will either need to declare the namespaces using xpath.namespace.config property or use functions like name() or local-name() functions instead of tag names
  3. In case of any troubles first of all check jmeter.log file - in the majority of cases it contains enough information to get to the bottom of the issue.

References:

Upvotes: 1

Ori Marko
Ori Marko

Reputation: 58902

To extract contents inside specific boundaries you can use Boundary Extractor:

Choose as Left boundary and </SFDCProductList> as Right boundary.

Put new variable name in Name of created variable as productList and use it later as ${productList} or vars.get("productList") inside a JSR223 script.

Allows the user to extract values from a server response using left and right boundaries. As a post-processor, this element will execute after each Sample request in its scope, testing the boundaries, extracting the requested values, generate the template string, and store the result into the given variable name.

Upvotes: 2

Related Questions