我正在嘗試使用 nodejs 代碼片段從 xml 讀取屬性值,下面是我的 xml 資料。嘗試獲取環境 QA 或 UAT。我撰寫了決議xml的代碼。現在挑戰無法理解console.log的這種輸出格式。不確定必須使用哪個 API 獲取/讀取“名稱”屬性值。
請幫我閱讀名稱屬性的值。
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="QA" ></testsuite>
<testcase status="success>"></testcase>
<testsuite name="QA2" ></testsuite>
<testcase status="success>"></testcase>
<testsuite name="UAT" ></testsuite>
<testcase status="success>"></testcase>
<testsuite name="UAT2" ></testsuite>
<testcase status="success>"></testcase>
<testsuite name="SandBox" ></testsuite>
<testcase status="success>"></testcase>
<testsuite name="SandBox" ></testsuite>
</testsuites>
`
const fs = require("fs");
const path = require("path")
const xml2js = require("xml2js")
let files;
let htmlobj = '<h3>Summary</h3><table><tr><th>Error Count</th></tr>'
files = fs.readdirSync(__dirname);
var parser = new xml2js.Parser();
let filecount = 0;
let counter = 0;
const junitfiles = {};
files.forEach(file => {
if (path.extname(file) != ".xml") return;
const extname = path.extname(file);
const filename = path.basename(file, extname);
const obsolutePath = path.resolve(__dirname, file);
fs.readFile(obsolutePath, function(err, data) {
if (err) {
console.log('error in readFile', err);
} else {
parser.parseString(data, function(err, result) {
if (err) {
console.log('error parsing', err)
}
else {
let resultvalue = 0;
let envcount = 0;
for (var i = 0; i < result.testsuites.testsuite.length; i ) {
resultvalue = result.testsuites.testsuite[i];
console.log('value of testsuite object', resultvalue);
}
}
});
}
});
});
`
Node js代碼的輸出
value of testsuite object { '$': { name: 'QA' } }
value of testsuite object { '$': { name: 'QA2' } }
value of testsuite object { '$': { name: 'UAT' } }
value of testsuite object { '$': { name: 'UAT2' } }
value of testsuite object { '$': { name: 'SandBox' } }
value of testsuite object { '$': { name: 'SandBox' } }
uj5u.com熱心網友回復:
要回答您的問題:
在下面添加 console.log('value of testsuite object', resultvalue);
const attributeName = resultvalue.$.name
console.log('value of attribute name is ', attributeName)
另一個注意事項:小心 .forEach 和回呼,因為它的異步代碼,你不會以正確的順序獲取資訊。
你認為你的代碼運行的順序
- 瀏覽檔案
- 回圈遍歷檔案 nr 1
- 讀取檔案編號 1
- 檔案 nr 1 的控制臺日志內容
- 回圈遍歷檔案 nr 2 ..etc 等。
但是回呼就像名字暗示的“當你準備好時給我回電話” - 所以它不會在讀取檔案之前控制臺記錄任何內容。同時回圈仍在繼續,所以你可以有類似的東西
- 瀏覽檔案
- 回圈遍歷檔案 nr 1
- 讀取檔案編號 1
- 回圈遍歷檔案 nr 2
- 檔案 nr 1 ..etc 等的控制臺日志內容。
看看專案 4 og 5 是如何切換的。
uj5u.com熱心網友回復:
這是使用 camaro 訪問測驗套件名稱的更簡單方法
const { transform } = require('camaro')
async function main() {
const xml = `
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="QA" ></testsuite>
<testcase status="success>"></testcase>
<testsuite name="QA2" ></testsuite>
<testcase status="success>"></testcase>
<testsuite name="UAT" ></testsuite>
<testcase status="success>"></testcase>
<testsuite name="UAT2" ></testsuite>
<testcase status="success>"></testcase>
<testsuite name="SandBox" ></testsuite>
<testcase status="success>"></testcase>
<testsuite name="SandBox" ></testsuite>
</testsuites>
`
template = {
names: ['testsuites/testsuite', '@name']
}
const { names } = await transform(xml, template)
console.log(names);
}
main()
輸出
[ 'QA', 'QA2', 'UAT', 'UAT2', 'SandBox', 'SandBox' ]
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/411837.html
標籤:
