I wrote a server in NodeJS that uses DymoJS to print labels to a Dymo printer. When printing, the XML for the label layout must be specified. The documentation outlines the following to declare the label XML:
var labelXml = `
<?xml version="1.0" encoding="utf-8"?>
<DieCutLabel Version="8.0" Units="twips">
...
<StyledText>
<Element>
<String>T</String>
<Attributes>
<Font Family="Helvetica" Size="13"
Bold="False" Italic="False" Underline="False" Strikeout="False"/>
<ForeColor Alpha="255" Red="0" Green="0" Blue="0"/>
</Attributes>
</Element>
<Element>
<String>EST123</String>
<Attributes>
<Font Family="Helvetica" Size="13"
Bold="False" Italic="False" Underline="False" Strikeout="False"/>
<ForeColor Alpha="255" Red="0" Green="0" Blue="0"/>
</Attributes>
</Element>
</StyledText>
</DieCutLabel>
`;
However in my XML, the <String>
elements are dependent on JS variables:
<Element>\
<String>${name}\n</String>\
...
</Element>\
The server I wrote is distributed across multiple endpoints, which use different printers, and therefore different labels, meaning they require different XML specifications.
What I would like, is to be able to load the basic XML from a document, and then replace ${name}
and other variables with their JS counterparts. I tried to use a raw fs.readfile()
, following this answer, which did not work, as the XML was not parsed as encoded (or that is my best guess as to what went wrong). Most of the rest of the questions I've found are either relating to Site-Mapped-JS, or end up converting the XML to JSON.
CodePudding user response:
Did you try to use xml-js?
You can edit structure as js objects and then translate it to xml.
For example:
const fs = require('fs');
const convert = require('xml-js');
const xml = fs.readFileSync('prueba.xml');
const js = convert.xml2js(xml, {compact: true});
js['DieCutLabel']['StyledText']['Element'][0]['String'] = 'Value of name';
console.log(convert.js2xml(js, {compact: true}));