TestComplete中使用JScript读取并修改XML数据问题咨询
No problem, let's walk through exactly how to update that CustomWeight value in your XML configuration using JScript in TestComplete. Here's a straightforward approach using the MSXML DOM object, which TestComplete supports natively:
Step 1: Load the XML Document
First, we'll load your XML file into a DOM document object so we can manipulate it. Make sure to replace the file path with your actual configuration file location.
// Initialize the XML DOM document var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.6.0"); xmlDoc.async = false; // Load synchronously to ensure full load before processing xmlDoc.validateOnParse = true; // Optional: Validate XML structure on load // Load the XML file var configPath = "C:\\Your\\Actual\\Path\\To\\Config.xml"; if (!xmlDoc.load(configPath)) { Log.Error(`Failed to load XML: ${xmlDoc.parseError.reason} (Error Code: ${xmlDoc.parseError.errorCode})`); return; }
Step 2: Locate the Target Node
We'll use an XPath query to directly find the <add> element with key="CustomWeight" under the MainScale device. This is more efficient than manually traversing all nodes:
// XPath query to target the exact CustomWeight parameter node var xpath = "//configuration/deviceConfiguration/devices/add[@logicalName='MainScale']/parameters/add[@key='CustomWeight']"; var targetNode = xmlDoc.selectSingleNode(xpath); if (targetNode === null) { Log.Warning("Could not find the CustomWeight parameter in the XML configuration"); return; }
Step 3: Update the Attribute Value and Save Changes
Once we have the node, we can modify its value attribute and save the updated XML back to the file:
// Update the value attribute targetNode.setAttribute("value", "1,20"); Log.Message("Successfully updated CustomWeight value from '0,20' to '1,20'"); // Save the changes back to the original file xmlDoc.save(configPath);
Key Notes
- XPath Precision: The XPath query targets the specific
MainScaledevice to avoid accidentally modifying other devices'CustomWeightvalues if they exist. - Permissions: Ensure TestComplete has write access to the XML file path—if it's in a protected directory (like Program Files), you might need to run TestComplete as administrator.
- MSXML Version: We're using
Msxml2.DOMDocument.6.0which is a stable, widely supported version. TestComplete should have access to this by default.
Putting it all together, the full script would look like this:
function UpdateCustomWeight() { var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.6.0"); xmlDoc.async = false; var configPath = "C:\\Your\\Actual\\Path\\To\\Config.xml"; if (!xmlDoc.load(configPath)) { Log.Error(`Load failed: ${xmlDoc.parseError.reason}`); return; } var xpath = "//configuration/deviceConfiguration/devices/add[@logicalName='MainScale']/parameters/add[@key='CustomWeight']"; var targetNode = xmlDoc.selectSingleNode(xpath); if (targetNode) { targetNode.setAttribute("value", "1,20"); xmlDoc.save(configPath); Log.Message("CustomWeight value updated successfully"); } else { Log.Warning("CustomWeight node not found"); } }
内容的提问来源于stack exchange,提问作者Jandak

