You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TestComplete中使用JScript读取并修改XML数据问题咨询

Modifying XML Attribute Value in TestComplete with JScript

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 MainScale device to avoid accidentally modifying other devices' CustomWeight values 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.0 which 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:10:23