Hi,
I have a requirement where I need to read an XML file inside my project through engine-side Javascript and store that value in a structure. The XML contains the screen name and its function keys list. However, I am not able to parse the XML file. Can anyone provide a solution for this?
Below I have attached the code for your reference
Engine Side Javascript:
-----------------------------
collect_FunctionKeys_From_Xml_File(screenName){
const path = 'I have the given the full path of the XML file here';
console.log("Is Exists....", fs.exists(' XML path'));
try {
const xmlString = fs.readFile(path);
console.log("xmlString.......", xmlString);
const parser = new DOMParser(); --> I am facing issue in this line. I am not able to parse the XML
const xmlDoc = parser.parseFromString(xmlString, "text/xml");
console.log("xmlDoc.......", xmlDoc);
// Find the screen with the matching name
const screens = xmlDoc.Screens.Screen;
const screen = screens.find(s => s.Name[0] === screenName);
if (screen && screen.FunctionKeys && screen.FunctionKeys[0].Key) {
const functionKeys = screen.FunctionKeys[0].Key.map(key => ({
fKey: key.FKey[0],
description: key.Description[0]
}));
}
console.log(Function Keys for ${screenName}:, functionKeys);
return functionKeys;
} catch (error) {
console.log("error", error);
}
}
Sample XML:
----------------
<FunctionKeysList>
<Screens>
<Screen>
<Name>Screen1</Name>
<FunctionKeys>
<Key>
<FKey>F1</FKey>
<Description>F1 Description</Description>
</Key>
</FunctionKeys>
</Screen>
<Screen>
<Name>Screen2</Name>
<FunctionKeys>
<Key>
<FKey>Help</FKey>
<Description>Help</Description>
</Key>
<Key>
<FKey>F3</FKey>
<Description>F3 Description</Description>
</Key>
<Key>
<FKey>F4</FKey>
<Description>F4 Description</Description>
</Key>
<Key>
<FKey>F5</FKey>
<Description>F5 Description</Description>
</Key>
<Key>
<FKey>F6</FKey>
<Description>F6 Description</Description>
</Key>
</FunctionKeys>
</Screen>
</Screens>
</FunctionKeysList>
Question
Read an XML and store the data in a structure using Engine side javascript
Sign up
Already have an account? Login
Welcome to the Rocket Forum!
Please log in or register:
Employee Login | Registration Member Login | RegistrationEnter your E-mail address. We'll send you an e-mail with instructions to reset your password.
