我正在尝试将以下xml转换为json,因此我需要获取到TS-tc-dt的映射 这是xml ?xml version="1.0" encoding="UTF-8"?TestScenario TestSuite name="TS_EdgeHome" TestCaseName name="tc_Login"dt_EdgeCaseHome,dt_EdgeCaseRoute/TestCase
这是xml
<?xml version="1.0" encoding="UTF-8"?> <TestScenario> <TestSuite name="TS_EdgeHome"> <TestCaseName name="tc_Login">dt_EdgeCaseHome,dt_EdgeCaseRoute</TestCaseName> <TestCaseName name="tc_Logout">dt_EdgeCaseRoute</TestCaseName> </TestSuite> <TestSuite name="TS_EdgePanel"> <TestCaseName name="tc_AddContract">dt_EdgeCaseHome,dt_EdgeCaseSpectrum</TestCaseName> </TestSuite> <TestSuite name="TS_EdgeRoute"> <TestCaseName name="tc_VerifyContract">dt_EdgeCaseRoute</TestCaseName> <TestCaseName name="tc_Payment">dt_EdgeCaseRoute</TestCaseName> </TestSuite> <TestSuite name="TS_EdgeSpectrum"> <TestCaseName name="tc_ClientFeedback">dt_EdgeCaseSpectrum</TestCaseName> </TestSuite> </TestScenario>
我怎样才能在NodeJS中实现这一目标?
您可以使用xml2json npm将xml转换为json. xml2json.第1步: – 在您的项目中安装包
npm install xml2json
第2步: – 您可以使用该包并将您的xml转换为json
let xmlParser = require('xml2json'); let xmlString = `<?xml version="1.0" encoding="UTF-8"?> <TestScenario> <TestSuite name="TS_EdgeHome"> <TestCaseName name="tc_Login">dt_EdgeCaseHome,dt_EdgeCaseRoute</TestCaseName> <TestCaseName name="tc_Logout">dt_EdgeCaseRoute</TestCaseName> </TestSuite> <TestSuite name="TS_EdgePanel"> <TestCaseName name="tc_AddContract">dt_EdgeCaseHome,dt_EdgeCaseSpectrum</TestCaseName> </TestSuite> <TestSuite name="TS_EdgeRoute"> <TestCaseName name="tc_VerifyContract">dt_EdgeCaseRoute</TestCaseName> <TestCaseName name="tc_Payment">dt_EdgeCaseRoute</TestCaseName> </TestSuite> <TestSuite name="TS_EdgeSpectrum"> <TestCaseName name="tc_ClientFeedback">dt_EdgeCaseSpectrum</TestCaseName> </TestSuite> </TestScenario>`; console.log('JSON output', xmlParser.toJson(xmlString));
希望这可能对你有所帮助.