JMeter如何提取并验证响应数据中指定的完整序列内容
Original Response Data
Here's the raw input XML (formatted for readability):
<?xml version="1.0"?> <PERSON> <NAME>Harry</NAME> <AGE>24</AGE> <REMARKS></REMARKS> <DETAILS> <GENDER>MALE</GENDER> <EYE_COLOR>BLUE</EYE_COLOR> </DETAILS> </PERSON> <?xml version="1.0"?> <PERSON> <NAME>Andrew</NAME> <AGE>4</AGE> <REMARKS></REMARKS> <DETAILS> <GENDER>MALE</GENDER> <EYE_COLOR>GREEN</EYE_COLOR> </DETAILS> <DETAILS> <WEIGHT>85KG</WEIGHT> <HEIGHT>173CM</HEIGHT> </DETAILS> </PERSON>
Target Sequence to Verify
The expected second PERSON entry (note the invalid closing tag for EYE_COLOR):
<?xml version="1.0"?> <PERSON> <NAME>Andrew</NAME> <AGE>4</AGE> <REMARKS></REMARKS> <DETAILS> <GENDER>MALE</GENDER> <EYE_COLOR>GREEN</COLOR> </DETAILS> <DETAILS> <WEIGHT>85KG</WEIGHT> <HEIGHT>173CM</HEIGHT> </DETAILS> </PERSON>
Efficient Solutions for Large Datasets
Since your data can be extremely large, we need methods that don’t load the entire file into memory. Below are two reliable approaches:
1. Command-Line (Awk)
This is fast and lightweight for processing huge files directly from the terminal. The command captures the second <PERSON> block and stops processing once it finds the closing </PERSON> tag to save resources.
awk '/<PERSON>/{count++} count==2{print; if(/<\/PERSON>/) exit}' your_input_file.xml
- How it works:
- Increments a counter every time it encounters
<PERSON> - Prints lines only when the counter is 2 (the second PERSON block)
- Exits immediately after the closing
</PERSON>to avoid unnecessary processing
- Increments a counter every time it encounters
2. Python (SAX Parser)
For more control (like validation), use Python’s SAX parser—an event-driven parser that processes XML incrementally without loading the entire document into memory.
Here’s a script to extract the second PERSON block and validate it against the target:
import xml.sax from xml.etree import ElementTree as ET class PersonHandler(xml.sax.ContentHandler): def __init__(self): self.current_person = [] self.in_person = False self.person_count = 0 self.target_found = False def startElement(self, name, attrs): if name == "PERSON": self.person_count += 1 self.in_person = True if self.person_count == 2: self.current_person.append(f"<{name}>") elif self.in_person and self.person_count == 2: self.current_person.append(f"<{name}>") def characters(self, content): if self.in_person and self.person_count == 2 and content.strip(): self.current_person.append(content.strip()) def endElement(self, name): if self.in_person and self.person_count == 2: self.current_person.append(f"</{name}>") if name == "PERSON" and self.person_count == 2: self.in_person = False self.target_found = True # Extract the second PERSON block handler = PersonHandler() xml.sax.parse("your_input_file.xml", handler) extracted_person = "\n".join(handler.current_person) # Parse target sequence (fix the invalid tag first if needed) target_xml = """<?xml version="1.0"?> <PERSON> <NAME>Andrew</NAME> <AGE>4</AGE> <REMARKS></REMARKS> <DETAILS> <GENDER>MALE</GENDER> <EYE_COLOR>GREEN</COLOR> </DETAILS> <DETAILS> <WEIGHT>85KG</WEIGHT> <HEIGHT>173CM</HEIGHT> </DETAILS> </PERSON>""" # Verification print("Extracted 2nd PERSON:") print(extracted_person) print("\nVerification Notes:") print("- The target sequence has an invalid closing tag `<\\/COLOR>` instead of `<\\/EYE_COLOR>`—this is not well-formed XML.") print("- Comparing the valid parts (excluding the typo):") try: # Parse extracted (valid) XML extracted_root = ET.fromstring(extracted_person) # Fix target typo to parse it corrected_target_xml = target_xml.replace("</COLOR>", "</EYE_COLOR>") target_root = ET.fromstring(corrected_target_xml) # Compare elements extracted_name = extracted_root.find("NAME").text target_name = target_root.find("NAME").text print(f" NAME matches: {extracted_name == target_name}") extracted_age = extracted_root.find("AGE").text target_age = target_root.find("AGE").text print(f" AGE matches: {extracted_age == target_age}") # Compare DETAILS elements extracted_details = extracted_root.findall("DETAILS") target_details = target_root.findall("DETAILS") print(f" Number of DETAILS blocks matches: {len(extracted_details) == len(target_details)}") except Exception as e: print(f" Parsing error: {e}")
Key Verification Observation
The target sequence contains an error: the closing tag for <EYE_COLOR> is written as </COLOR> instead of </EYE_COLOR>. This makes the target XML invalid. When verifying, you’ll need to correct this typo first to compare the content properly.
内容的提问来源于stack exchange,提问作者Taz 3497

