How to install third party libraries?
You can use the third party libraries in Smartface.
XML Parsing in Smartface
If you want to work with XML data, you can use the third party xml parser library.
Example library : https://github.com/NaturalIntelligence/fast-xml-parser
Install library on Smartface :
(cd /projects/workspace/scripts/node_modules && npm install fast-xml-parser)
XML Parse Example :
import fastXmlParser from "fast-xml-parser";
const xmlData =
"<bookstore><book>" +
"<title>Everyday Italian</title>" +
"<author>Giada De Laurentiis</author>" +
"<year>2005</year>" +
"</book></bookstore>";
let JsonResult = fastXmlParser.parse(xmlData);
console.log("JSON Data : " + JSON.stringify(JsonResult));
// when a tag has attributes
const options = {
attrPrefix: "@_",
attrNodeName: false,
textNodeName: "#text",
ignoreNonTextNodeAttr: true,
ignoreTextNodeAttr: true,
ignoreNameSpace: true,
ignoreRootElement: false,
textNodeConversion: true,
textAttrConversion: false,
arrayMode: false,
};
if (fastXmlParser.validate(xmlData) === true) {
//optional
JsonResult = fastXmlParser.parse(xmlData, options);
console.log("JSON Data : " + JSON.stringify(JsonResult));
}
//Intermediate obj
const tObj = fastXmlParser.getTraversalObj(xmlData, options);
JsonResult = fastXmlParser.convertToJson(tObj);
console.log("JSON Data : " + JSON.stringify(JsonResult));
JSON Parsing in Smartface is no different than JSON parse in Javascript.
try {
const JsonResult = JSON.parse(data); // data is string.
console.log("JSON Data : " + JSON.stringify(JsonResult));
} catch (e) {
console.error("JSON parse failed.");
}